Abweichung
Abweichung

Reputation: 97

If/Else calculations through JOptionPane

I am trying to calculate this if/else statement through the JOption Message window. Prompt 1 is fine and returns the name, but I am not getting any calculations through prompt 2. I'm guessing I need to do something with my double number1, but I'm not exactly sure.

Thanks in advance!

public static void main(String[] args)
{

    Scanner imput = new Scanner(System.in);   //ALLOW USER TO INPUT NAME AND MONTHLY INCOME

    long monthlyIncome = 0;
    double newSocSecTax = 0.0;
    double oldSocSecTax = 0.0;

    String name = JOptionPane.showInputDialog( "Please enter your name: "); //PROMPT 1

    String income = JOptionPane.showInputDialog("\nPlease enter your gross monthly income:  "); //PROMPT 2
    double number1 = Double.parseDouble( income );

    if(monthlyIncome >= 110100.00)
    {
       newSocSecTax = 110100.00 * .062;
       oldSocSecTax = 106800.00 * .042;
    }
    else
    {
       if(monthlyIncome >= 106800.00)
       {
         newSocSecTax = monthlyIncome * .062;
         oldSocSecTax = 106800.00 * .042;
       }
   else
   {
       newSocSecTax = monthlyIncome * .062;
       oldSocSecTax = monthlyIncome * .042;
   }
   }


   String message = String.format( "%s, your monthly social security tax will be $%.2f." +
                                 "\nThis is an increase of $%.2f.", name, newSocSecTax, oldSocSecTax );

   JOptionPane.showMessageDialog( null, message );
   System.exit(0);
}

}

Upvotes: 0

Views: 3106

Answers (4)

RanRag
RanRag

Reputation: 49587

Change your code to

double monthlyIncome = Double.parseDouble( income );

because in if-else statement you are using monthlyIncome but in your original code you assigned the value to number1

Upvotes: 0

Thomas
Thomas

Reputation: 88747

I assume you want to assign number1 to monthlyIncome, don't you? You're not using number1 thus far and monthlyIncome will still have the value 0 after the user entered the number.

Keep in mind that monthlyIncome is a long variable and thus assigning number1 will require a cast and might result in precision losses (if the user enters fractions), although those might be acceptable in your case.

Upvotes: 0

John3136
John3136

Reputation: 29266

you haven't set monthlyIncome to be anything based on the number1 returned through the "prompt".

Upvotes: 1

Dave Newton
Dave Newton

Reputation: 160291

You don't have a value in monthlyIncome, you parse the input into number1. This leaves monthlyIncome at 0, making the tax variables also 0.

Note that it's helpful to describe what's actually happening versus what you expect to happen, otherwise we're forced to guess what you mean by "not getting any calculations". Of course you're getting calculations--the problem is that they're not what you want.

Upvotes: 1

Related Questions