I have my code here, which allows the user to enter an amount for income. My custom made exception (negativeException) allows me to enter the income after it tells me that no negative exceptions are allowed. I used the same code with a few changes to handle an InputMismatchException. The thing is when I enter an InputMismatchException, it skips the part where I get to reenter my input. It skips ahead to the finally statement and prints out the System.out.println(txp). I'm only posting the code that I feel is relevant to the problem, if you need to see more let me know please.
Really all I've tried is moving things around in different areas. Most of the time it just crashes the program.
Main:
public class callTaxCode_Test {
static float income;
public static void main(String[] args) throws negativeException{//, InputMismatchException{
//GUI will consist of: one text field for income
// drop down box with each type (6 total)
// enter button, tax cash button
Scanner kb = new Scanner(System.in);
System.out.println("Enter Type");
String input = kb.nextLine();
System.out.println("Income: ");
if(input.equalsIgnoreCase("corp")) {
try {
income = kb.nextFloat();
corporation c = new corporation(input, income);
}
catch(InputMismatchException e) {
System.out.println("Invalid Input!");
System.out.println("Please Enter the correct income: ");
income = kb.nextFloat();
//corporation c = new corporation(input, income);
}
catch(negativeException e) {
System.out.println(e.getMessage());
System.out.println("Please Enter the correct income: ");
income = kb.nextFloat();
corporation c = new corporation(input, income);
}
finally {
corporation c = new corporation(input, income);
taxpayerInfo<Float> txp = new taxpayerInfo<Float>(taxpayer.getIncome(),
taxpayer.getFlatRate(),
taxpayer.getTaxRate(),
taxpayer.getIncome());
txp.setType(input);
txp.calcTax();
System.out.println(txp);
}
}
Corporation class:
public class corporation extends taxpayer{
//for corporations taxable income is 100% of income
//there is no flat rate so we will set that to 0
//tax rate is 21%
//tax = .21 \* income
public corporation(String type, float income) throws negativeException{
setType(type);
setIncome(income);
flatRate = 0;
taxRate = .21F;
taxableIncome = income;
}
}
taxpayer (which serves as my super class):
public class taxpayer{
//corp, trust, person can all follow the same formula
//tax = flat rate + (tax rate \* taxable income)
//corp's flat rate is 0, and its taxable income is 100% of income
//also has a set tax rate of 21%
//tax = 0 + (.21 \* income)
//trust + person taxable income is determined by the excess amount of income
//after the brackets minimum income allowing them into the bracket
[//e.g](//e.g). It takes 38,700 to belong in the 3rd bracket of a single filer
//taxable income = income - 38700
//everything else in these classes are determined by the bracket their income places them
protected static String type;
protected static float income;
protected static float flatRate;
protected static float taxRate;
protected static float taxableIncome;
//get income
public static float getIncome() {
return income;
}
//set income, throw exception if negative
public static void setIncome(float income) throws negativeException{
if(income < 0) {
throw new negativeException();
}
else {
taxpayer.income = income;
}
}
//set type
public static void setType(String type) throws InputMismatchException{
taxpayer.type = type;
}
Negative exception Class:
public class negativeException extends Exception {
public negativeException() {
super("Negative Values are not allowed!");
}
}
What I expect is for my InputMismatchException to behave like my negativeException. That is when it catches an excpetion is allows me to enter a new income, which negativeException allows me to do. However, it skips allowing me to reenter income, and skips ahead to the finally statement.