0

I'm doing this java homework of finding the tax income based on marital status using the if else statements and joptionpane. The issue is after getting the tax and marital status inputs the output doesn't show up it just terminates the program. There no errors also.

I tried putting a semicolon after the first if statements and it somehow works but it shows 2 different outputs instead of 1


public class Program_4_2 {

public static void main (String[] args) {

String status;
String tax;
double tax_income;

status = JOptionPane.showInputDialog("Please enter s for single, m for married: ");
tax = JOptionPane.showInputDialog("Enter Your Tax Income: ");
tax_income = Double.parseDouble(tax);

// SINGLE TAX;
if (status == "s")
{
    if (tax_income > 0 && tax_income < 8000)
        {
        tax_income = tax_income * 0.1 + 0;
        JOptionPane.showMessageDialog(null, "Your Tax is " + tax_income);
        }

    else if (tax_income > 8000 && tax_income < 32000)
        {
            tax_income = (tax_income - 8000) * 0.15 + 800;
            JOptionPane.showMessageDialog(null, "Your Tax is " + tax_income);
        }
    else if (tax_income > 32000)
        {
            tax_income = (tax_income - 32000) * 0.25 + 4400;
            JOptionPane.showMessageDialog(null, "Your Tax is " + tax_income);
        }
}

// Married Tax
if (status == "m")
{
    if (tax_income > 0 && tax_income < 16000)
        {
            tax_income = tax_income * 0.1 + 0;
            JOptionPane.showMessageDialog(null, "Your Tax is " + tax_income);
        }

    else if (tax_income > 16000 && tax_income < 64000)
        {
            tax_income = (tax_income - 16000) * 0.15 + 1600;
            JOptionPane.showMessageDialog(null, "Your Tax is " + tax_income);
        }
    else if (tax_income > 64000)
        {
            tax_income = (tax_income - 64000) * 0.25 + 8800;
            JOptionPane.showMessageDialog(null, "Your Tax is " + tax_income);
        }
}

}
}

An example output would be :

user input status -> "s" -> user then inputs tax -> "9000" -> output should show -> "950" or "900" if the status input is m

  • It works now! Don't know why this didn't work awhile ago, but tried it again and now it's working. Thank you! – SirLancelot54 Sep 11 '19 at 23:04
  • Also: either your ```<``` should be ```<=``` or your ```>``` should be ```>=```. Consider, for example, the case of filing single, income is 8000 exactly. –  Sep 11 '19 at 23:16
  • I changed some after having a wrong output while testing other data inputs. Thanks! – SirLancelot54 Sep 11 '19 at 23:24
  • If you write `if (condition); { block }` the `;` is the empty statement and the block will be run *unconditionally*. – Stephen C Sep 12 '19 at 00:05

1 Answers1

0

It looks like you got it working, but for future reference don't use == to compare Strings.

The correct way to compare strings is with .equals like so:

status.equals("m")
AussieGuy
  • 21
  • 2