0

Hi I needed help with doing a calculation in this right here. On the line where it has single, i want to add the number to the calculation but it just adds it as if im just writing the number itself in a sentence. How do i add it as if its a calculation, im very stuck and need help. (Its the last line of code). I also wanted to know how i can limit the amount of letters a user can input, since i only want them to either enter 's' or 'm'. How can i limit them to only these two so they dont use a letter like 'g' for example, since that wont work.

import java.util.Scanner; 
public class FedTaxRate 
{
 public static void main(String[] args)
 {
  String maritalStatus; 
  double income; 
  int single = 32000; 
  int married = 64000;

  Scanner status = new Scanner(System.in);
  System.out.println ("Please enter your marital status: "); 
  maritalStatus = status.next();


  Scanner amount = new Scanner(System.in);
  System.out.print ("Please enter your income: ");
  income = amount.nextDouble();

  if (maritalStatus.equals("s") && income <= 32000 )
  {
     System.out.println ("The tax is " + income * 0.10 + ".");
  }
  else if (maritalStatus.equals("s") && income > 32000) 
  {
     System.out.println ("The tax is " + (income - 32000) * 0.25 + single + ".");
  }

  }
 }
McDodger
  • 43
  • 1
  • 1
  • 7
  • Use brackets, `System.out.println ("The tax is " + (income * 0.10) + ".")` – MadProgrammer Oct 05 '15 at 01:53
  • the product of that line was correct im asking about this one: . else if (maritalStatus.equals("s") && income > 32000) { System.out.println ("The tax is " + (income - 32000) * 0.25 + single + "."); } – McDodger Oct 05 '15 at 01:56
  • Same thing, wrap the entire calculation with in brackets – MadProgrammer Oct 05 '15 at 01:57
  • thank you soooo much, i tried what you said before but in a different way but thank you i got it, and can u help me with my second question? – McDodger Oct 05 '15 at 02:02
  • You can't really, you could simply ignore anything after the first character, using `substring` or `startsWith` for example – MadProgrammer Oct 05 '15 at 02:16

2 Answers2

1

To answer your second question about limiting the input, you can try using a switch case statement. The default allows you to write code for the case when maritalStatus is not equal to either "s" or "m". You can also create a do-while loop to keep asking for input until maritalStatus is equal to either "s" or "m".

Scanner status = new Scanner(System.in);
String maritalStatus = status.nextLine();

do {
    System.out.println("Enter your marital status.")
    switch (maritalStatus) {
        case "s": 
            // your code here
            break;
        case "m":
            // your code here
            break;
        default:
            // here you specify what happens when maritalStatus is not "s" or "m"
            System.out.println("Try again.");
            break;
        }
    // loop while maritalStatus is not equal to "s" or "m"
    } while (!("s".equalsIgnoreCase(maritalStatus)) && 
             !("m".equalsIgnoreCase(maritalStatus))); 
Dan Zheng
  • 1,493
  • 2
  • 13
  • 22
0

You only need one Scanner. You can use an else with your income test. I would suggest you calculate the tax once, and then display it with formatted IO. Something like,

public static void main(String[] args) {
    int single = 32000;
    int married = 64000;
    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter your marital status: ");
    String maritalStatus = sc.next();

    System.out.print("Please enter your income: ");
    double income = sc.nextDouble();
    double tax;
    if ("s".equalsIgnoreCase(maritalStatus)) {
        if (income <= single) {
            tax = income * 0.10;
        } else {
            tax = (income - single) * 0.25 + single;
        }
    } else {
        if (income <= married) {
            tax = income * 0.10;
        } else {
            tax = (income - married) * 0.25 + married;
        }
    }
    System.out.printf("The tax is %.2f.%n", tax);
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249