Reputation: 47
I am pretty new to Java. I am doing a program that calculates an Income Tax summary based on one's marital status. My program works, aside from one problem: my input validation section for the marital status is not working properly. When I enter something other than s, S, m, M, c, and C, the program is supposed to perform input validation for that, but currently, when I enter something like x, it skips the gross income and exemptions, and spits out that tax rate, taxes owed, and taxable income are all 0.
EDIT: Sorry if that was confusing. Basically, if the user uses invalid input, and the user then enters something valid, I want to know how to make it go back to the beginning of the switch statement to determine the tax rate Any help would be appreciated!
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("_ _ _ _ _ ' 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
do{ //continues to ask for valid input until user inputs a valid marital status
System.out.print("\nInvalid entry.");
System.out.print("\nAre you (s)ingle, (m)arried, or (c)ohabiting?");
System.out.print("\nEnter s, m, or c ==> ");
maritalStatus = sc.next().charAt(0);
} while (maritalStatus != 's' && maritalStatus != 'S' && maritalStatus != 'm' && maritalStatus != 'M' && maritalStatus != 'c' && maritalStatus != 'C');
}
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
}
}
Upvotes: 0
Views: 364
Reputation: 1503
You need to have the switch-Statement inside the do-while-loop you got in default in your example, like
boolean martialStatusValid = true
maritalStatus = sc.next().charAt(0);
do {
martialStatusValid = true;
switch (maritalStatus) {
//your cases here
default:
martialStatusValid = false;
//display some kind of error message ...
}
} while (! martialStatusValid)
That way, as long as the martialStatus is invalid the User will be prompted for new input and the switch will repeat.
One thing to note abound Launes "continue-solution: that will start the next iteration of the outer do-loop, so basically restart your Program. Might not be what you're after if you have more Sections with user input, f.e. if you want to ask for sex etc.
Upvotes: 0
Reputation: 31290
Instead of retrying after
default: //if input for marital status is invalid
do{ //continues to ask for valid input until user inputs a valid marital status
} while(...);
simply skip to the end of the outer loop:
default:
System.out.print("\nInvalid entry.");
continue;
Upvotes: 1