-
Error in coding
Hi, I am taking my first course in java. could you help me to find the problem here.... thanks
Code:
import javax.swing.JOptionPane;
public class FixDebugTwo4
{
public static void main(String[] args)
{
String costString;
double cost = JOptionPane.showInputDialog(null, "Enter price of item you are buying");
double tax = 0.06;
JOptionPane.showMessageDialog(null, "With tax your purchase is " + (cost * tax) + cost);
System.exit(0);
}
}
-
Welcome to DevX 
What am I looking for?
What is the error?
I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
Please use [Code]your code goes in here[/Code] tags when posting code.
Before posting your question, did you look here?
Got a question on Linux? Visit our Linux sister site.
Modifications Required For VB6 Apps To Work On Vista
-
error in coding
Thanks Hack:
the first one is: if you do the math and the product cost $2 +tax the price will be $2.12 and not like 26. the second is when you try to compile it:
DebugTwo4.java:15: ')' expected
System.exit(0);
thanks
-
Think about your math - and enclose your calculation inside parenthesis at the end of the error generating statement so that only the result is included in your output string.
Wouldn't it be better to calculate the final cost as (1 + tax) * cost?
-
error in coding
Thanks Hack,
Good idea. I have to review my math, yes. Now sorry to bother you again but the same idea with this one (I am debugging) but the final result is "With 6.0 tax, purchase is $1.00.06" but shouldn't be the answer $1.06? how I limit or erase that 00 extra. will it be in the declaration of variables?
Thanks again, I am learning a lot with this.
Code:
import javax.swing.JOptionPane;
public class DebugTwo4
{
public static void main(String[] args)
{
String costString;
double cost;
double tax = 0.06;
costString = JOptionPane.showInputDialog(null,
"Enter price of item you are buying", "Purchases",
JOptionPane.INFORMATION_MESSAGE);
cost = Double.parseDouble(costString);
JOptionPane.showMessageDialog(null,"With " + tax * 100 +
"% tax, purchase is $" + cost + cost * tax);
System.exit(0);
}
}
-
You are seeing the results of passing the string representation of the cost and then concatenating the result of cost*tax; therefore you are getting 1.00.06.
If, however, you put your entire calculation inside of a parenthesis, the compiler knows that you want to do the math and THEN pass the string representation of the RESULT to the output. Replace the existing end of the statement to
...+ (cost + (cost * tax)) );
and the math will be performed and only the result will be displayed.
-
error in coding
Thanks Nspils,
It worked wonderfully. since this is my first java course and on line, it has been a little hard but I think I am starting to understand all this things about strings, class, concatenation, etc.
thanks again for supporting people like me, beginners. no more for now. bye.
Similar Threads
-
By united_1987 in forum VB Classic
Replies: 2
Last Post: 11-27-2006, 03:04 AM
-
By Mohammedali1989 in forum Java
Replies: 1
Last Post: 12-01-2005, 11:09 PM
-
By LogicsHere in forum Java
Replies: 1
Last Post: 09-23-2005, 10:13 AM
-
By bhowerton in forum Database
Replies: 4
Last Post: 05-24-2005, 06:31 AM
-
By Tuomas Salste in forum vb.announcements
Replies: 0
Last Post: 01-24-2001, 12:22 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks