Sherene Kemp
Sherene Kemp

Reputation: 21

Java error with double variables

I'm attempting to add interactive input to a program. I've been working on it for hours and can't figure it out. Here is the code -

// This program calculates an employee's take home pay. 
    public class Payrolls
        import javax.swing.JOptionPane;

    {
        public static void main(String args[])
        {
            String salaryString;
            double salary;
            double stateTax;
            double federalTax;
            String numDependentsString;
            double numDependents;
            double dependentDeduction;
            double totalWithholding;
            double takeHomePay;

            salaryString = JOptionPane.showInputDialog ("Enter Salary Here: ");
            salary = double.parseDouble (salaryString);
            numDependentsString = JOptionPane.showInputDialog     ("Enter Number of Dependents: ");
            numDependents = double.parseDouble (numDependentsString);

            // Calculate state tax here.
            stateTax = salary * .06;

            System.out.println("State Tax: $" + stateTax);
            // Calculate federal tax here. 
            federalTax = salary * .25;

            System.out.println("Federal Tax: $" + federalTax);
            // Calculate dependant deduction here.
            dependentDeduction = (salary * .02) * numDependents;

            System.out.println("Dependents: $" + dependentDeduction);
            // Calculate total withholding here.
            totalWithholding = stateTax + federalTax;

            // Calculate take home pay here.
            takeHomePay = salary - totalWithholding + dependentDeduction;

            System.out.println("Salary: $" + salary);
            System.out.println("Take Home Pay: $" + takeHomePay);
            System.exit(0);
        }
    }

I have 9 errors -

Payrolls.java:19: error: class expected
                salary = double.parseDouble (salaryString);
                                ^
Payrolls.java:19: error: ';' expected
                salary = double.parseDouble (salaryString);
                                           ^
Payrolls.java:19: error: not a statement
                salary = double.parseDouble (salaryString);
                                             ^
Payrolls.java:19: error: ';' expected
                salary = double.parseDouble (salaryString);
                                                         ^
Payrolls.java:21: error: class expected
                numDependents = double.parseDouble (numDependentsString);
                                       ^
Payrolls.java:21: error: ';' expected
                numDependents = double.parseDouble (numDependentsString);
                                                  ^
Payrolls.java:21: error: not a statement
                numDependents = double.parseDouble (numDependentsString);
                                                    ^
Payrolls.java:21: error: ';' expected
                numDependents = double.parseDouble (numDependentsString);

Upvotes: 2

Views: 1085

Answers (8)

Kausik Jana
Kausik Jana

Reputation: 11

Instead of double please use Double. Java is pure case sensitive language. double is a keyword which indicates the data type of a variable and has no methods. Double is class in java and has method like parseDouble(String str). This method returns the double value of a string value.

salary = double.parseDouble(salaryString);
numDependentsString = JOptionPane.showInputDialog("Enter Number of Dependents: ");
numDependents = double.parseDouble(numDependentsString);

just keep coding up!

Upvotes: 1

Bill K
Bill K

Reputation: 62769

Java has very very helpful errors, just read them carefully.

The 19 means line number 19, then just read what it's telling you carefully. You usually don't have to read past the first few lines of the error printout. Only fix that one problem, and when you fix it, compile again and hopefully it will be a different error to solve. Repeat until you run out of errors.

I know there is a tendency to ignore the messages because they are so long, but seriously, they work.

Now, to make things easier, download Eclipse. It's free and easy--it will set up quickly. Create your program in Eclipse. It will underline every error and when you put your mouse over the error line it will tell you exactly what's wrong.

It really makes things much easier.

Upvotes: 0

Ewald
Ewald

Reputation: 5751

remember, Java is case-sensitive. So, it will treat double and Double as two separate items. In this case, you are trying to use Double, but have specified double by mistake. This is a very common Java problem, don't feel bad about it, just take note that Java is case-sensitive. Happy coding!!

Now, to solve the problem:

The line

 salary = double.parseDouble (salaryString);

should read

 salary = Double.parseDouble (salaryString);

The same goes for the "numDependents = Double.parseDouble (numDependentsString);" line as well.

Upvotes: 1

Debobroto Das
Debobroto Das

Reputation: 862

 salary = double.parseDouble (salaryString);

change it to

salary = Double.parseDouble (salaryString);

Upvotes: 0

stealthjong
stealthjong

Reputation: 11093

Use the wrapperclass from the double

Double.parseDouble(salaryString)

Upvotes: 0

Jimmy
Jimmy

Reputation: 2619

You don't have any problem anywhere else then in this code:

 salaryString = JOptionPane.showInputDialog ("Enter Salary Here: ");
    salary = Double.parseDouble (salaryString);
    numDependentsString = JOptionPane.showInputDialog     ("Enter Number of Dependents: ");
    numDependents = Double.parseDouble (numDependentsString);

Instead of using wrapper class Double, you were using double.parseDouble.

Replace your code with above , you will be fine.

I mean use Double.parseDouble instead.

Upvotes: 0

sgpalit
sgpalit

Reputation: 2686

Try Double.parseDouble not double.parseDouble

Upvotes: 3

HJW
HJW

Reputation: 23443

Double.parseDouble (salaryString);

Its Double.parseDouble, not double.parseDouble. :)

The parseDouble method comes from the Double class.

Upvotes: 5

Related Questions