Click here to Skip to main content
15,902,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
import javax.swing.JOptionPane;


public class Text

	{
		public static void main(String[] args)
		{
		String amount;
		double stateSalesTax;
		double countySalesTax;
		double totalSalesTax;
		double totalSalesAmount;
		double amt;
		JOptionPane.showInputDialog("Enter the amount of purchase");
		amt = Double.parseDouble(amount);
		
		stateSalesTax = 0.055;
		countySalesTax = 0.02;
		totalSalesTax = stateSalesTax + countySalesTax;
		totalSalesAmount = amt + totalSalesTax;
		
		JOptionPane.showMessageDialog (null, "The purchase amount is: " + amt + 
										"The state sales tax is:" + stateSalesTax + 
										"The county sales tax is:" + countySalesTax +
										"The total amount of sales is:" + totalSalesAmount);
		}
	}


Command prompt tells me my problem is on the line that reads amt = Double.parseDouble(amount);

What I have tried:

I have tried to use a cast operator to tell the program that the string type can coexist with the double type, but alas, it was to no avail.
Posted
Updated 10-Oct-16 21:39pm
v2
Comments
[no name] 10-Oct-16 18:18pm    
What "problem" are you talking about? We can't see your screen from here. amount is probably null. null is not a double.

Use the debugger to see what is the value of amount at error position.
Quote:
I have tried to use a cast operator to tell the program that the string type can coexist with the double type, but alas, it was to no avail.
Obviously, you don't understand how it works, further study is in order.


You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Here is working code.
Two errors amount is not defined and second thing is that you need to import javax.swing.JOptionPane;

Java
<pre lang="java">
import javax.swing.JOptionPane;

public class Text {

    public static void main(String[] args) {
        String amount;
        double stateSalesTax;
        double countySalesTax;
        double totalSalesTax;
        double totalSalesAmount;
        double amt;
        amount = JOptionPane.showInputDialog("Enter the amount of purchase");
        amt = Double.parseDouble(amount);

        stateSalesTax = 0.055;
        countySalesTax = 0.02;
        totalSalesTax = stateSalesTax + countySalesTax;
        totalSalesAmount = amt + totalSalesTax;

        JOptionPane.showMessageDialog(null, "The purchase amount is: " + amt
                + "The state sales tax is:" + stateSalesTax
                + "The county sales tax is:" + countySalesTax
                + "The total amount of sales is:" + totalSalesAmount);
    }
}
 
Share this answer
 
Comments
Richard MacCutchan 11-Oct-16 5:55am    
That is not correct as you are using absolute values for sales tax, rather than calculated values based on the amount.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900