1

I am sorry to post again so soon, but I ran into another error in my Tax program. My program is only supposed to work if someone enters only 's,' 'S,' 'm,' 'M,' 'c,' or 'C' for the marital status. When I entered anything that does not start with one of those letters, it prints Invalid Entry, like it is supposed to, but when I enter something that starts with an upper/lowercase s, m, or c, it proceeds as if I only entered that first letter. How do I make it so that the program only proceeds if you enter the one letter? Thanks!!

Here is my code:

import java.util.Scanner;

public class TaxPrep
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);

        char maritalStatus;
        double grossIncome = 0;
        double numberOfExemptions = 0;
        double taxRate = 0;
        double taxableIncome = 0;
        double taxesOwed = 0;
        String anotherCustomer = "y";

        System.out.print("A A R O N ' S  T A X  P R E P A R E R\n\n");

        do{
            System.out.print("Are you (s)ingle, (m)arried, or (c)ohabiting?\n");
            System.out.print("Enter s, m, or c ==> ");
            maritalStatus = sc.next().charAt(0);
            switch (maritalStatus)
            {
                case 's': case 'S':
                    System.out.print("Gross income ==> ");
                    grossIncome = sc.nextDouble();
                    System.out.print("Number of exemptions ==> ");
                    numberOfExemptions = sc.nextInt();
                    taxableIncome = grossIncome - (1000 * numberOfExemptions);
                    taxRate = 20;
                    break;
                case 'm': case 'M':
                    System.out.print("Gross income ==> ");
                    grossIncome = sc.nextDouble();
                    System.out.print("Number of exemptions ==> ");
                    numberOfExemptions = sc.nextInt();
                    taxableIncome = grossIncome - (1000 * numberOfExemptions);
                    taxRate = 25;
                    break;
                case 'c': case 'C': //tax rate for cohabiting depends on taxable income
                    System.out.print("Gross income ==> ");
                    grossIncome = sc.nextDouble();
                    System.out.print("Number of exemptions ==> ");
                    numberOfExemptions = sc.nextInt();
                    taxableIncome = grossIncome - (1000 * numberOfExemptions);
                    if (taxableIncome <= 20_000)
                    {
                        taxRate = 10;
                        break;
                    }
                    else if (taxableIncome <= 50_000)
                    {
                        taxRate = 15;
                        break;
                    }
                    else
                    {
                        taxRate = 30;
                        break;
                    }
                default: //if input for marital status is invalid
                    System.out.print("\nInvalid entry.\n");
                    continue;
            } 

            taxesOwed = taxableIncome * (taxRate / 100);

            //taxable income and taxes owed cannot be negative
            if (taxableIncome < 0)
            {
                taxableIncome = 0;
            }
            if (taxesOwed < 0)
            {
                taxesOwed = 0;
            }

            //tax summary
            System.out.print("\nINCOME TAX SUMMARY");
            System.out.print("\ntax rate: " + taxRate + "%");
            System.out.print("\ntaxable income: $" + taxableIncome);
            System.out.print("\ntaxes owed: $" + taxesOwed);

            //would you like to process another customer?
            System.out.print("\n\nProcess another customer? (y/n): ");
            anotherCustomer = sc.next();
            System.out.print("\n");
        } while (anotherCustomer.equalsIgnoreCase("y")); //as long as user enters 'y' or 'Y', the program will continue to calculate the income tax summary
    }
}

1 Answers1

3

You are testing only the first character, even if the entered word is longer:

maritalStatus = sc.next().charAt(0);

sc.next() returns a String, and charAt(0) returns just its first character. So if you entered "sFoo", maritalStatus would just be 's' and the "Foo" would be ignored.

You should get the whole entered String (and possibly convert it to upper case for easier comparison in the switch). The variable maritalStatus should be declared as String in this case:

maritalStatus = sc.next().toUpperCase();

switch (maritalStatus) {
case "S":
    //...
}

Switch statement works with Strings, unless you are using Java 6 or older. In that case you would use:

maritalStatus = sc.next().toUpperCase();

if (maritalStatus.equals("S")) {
    //...
} else if...
Cinnam
  • 1,872
  • 1
  • 13
  • 23
  • I think that we are using Java 6, but not sure. Are you saying to convert the chars to strings? – IronMaiden28 Oct 31 '15 at 20:47
  • @IronMaiden28 I have updated my answer. And yes, `maritalStatus` should be `String`. – Cinnam Oct 31 '15 at 20:53
  • What does the .toUpperCase() do? – IronMaiden28 Oct 31 '15 at 21:22
  • @IronMaiden28 See [`String#toUpperCase()`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#toUpperCase--) - converts the `String` to upper case, so you only need e.g. `case "S":` instead of `case "s": case "S":` – Cinnam Oct 31 '15 at 21:27