0

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.

kryger
  • 11,746
  • 8
  • 41
  • 60
  • Can you include the code for your `InputMismatchException`, as well as an example of where you were creating and throwing the `InputMismatchException`? – Jordan Apr 12 '19 at 20:25
  • Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – markspace Apr 12 '19 at 20:30
  • Besides the possibility that `nextFloat` is tripping you up, the other thing that you're doing is you're not looping to read the input again if you get an exception. You try to read the value once more but if the input isn't correct the second time then you're sunk. – markspace Apr 12 '19 at 20:32
  • @Jordan I did not make create anything for InputMismatchException. I just used the built in one. Every where I throw it is included in the code above. – meepz117 Apr 12 '19 at 22:15
  • @markspace So if I toy around with my nextFloat() methods I may get what I want? From what I understood from the post you linked, something is consuming an empty line as input. This would make sense, because my one line System.out.println(txp) is supposed to show a toString method, which is showing everything as zero. I think I'm getting a better idea of what is going on. Can you give me an idea to how I'm supposed to write this loop? Am I looping it back to my original nextFloat()? Or am I looping it within the InputMismatchException block? – meepz117 Apr 12 '19 at 22:25
  • Unfortunately the more I look at your code the more bugs I see. Your `taxpayer` super-class has a lot of static methods and fields. That basically can't work. You're going to have to re-write large sections of this code. I'd recommend asking a question about how to make a base class, using your `taxpayer` and `corporation` as examples. Starting in smaller steps I think is the only way to get the whole thing working. – markspace Apr 13 '19 at 00:45

0 Answers0