0

I was doing my assignment and paycode is one of the variable that let user to key in. So this paycode i declare it as int. So what if the user type other than integer such as a,b,c,d or 1.5,2.5 and so forth. I try the if else function but when user input other than integer there will be error in it. And I wan to make it loop also when user key in paycode other than the integer and let user key in again. This is all I have now and i can't make it loop. Instead, it will turn into infinity loop.

int paycode;
boolean good = true;

do {
    try {
        System.out.printf("Key in the paycode of the worker according to the worker type: \n");         
        paycode = myscanner.nextInt();  

        if (paycode == 4 || paycode == 3 || paycode == 2 || paycode == 1) {
            good = false;
        } 
        else { 
            //user key in out of the range of pay code
            System.out.printf("Paycode must be 1,2,3,4 \n");
        }
    }
    catch(InputMismatchException e) {
        System.out.printf("Paycode cannot have decimal places. \n");
        System.out.printf("Key in the paycode of the worker according to the worker type: \n"); 
    }   
}       
while (good);
5
  • 2
    JavaScript != Java Commented Dec 7, 2015 at 3:24
  • Are you using java or javascript? Your question says javascript, but your code is java. Commented Dec 7, 2015 at 3:24
  • Mate this is not Javascript Commented Dec 7, 2015 at 3:25
  • thats Java not Javascript Commented Dec 7, 2015 at 3:25
  • Oh I misclick the tag because it come out automatically after i finish typing everything and didn't notice it's javascript. Commented Dec 7, 2015 at 3:47

1 Answer 1

2

When myscanner attempts to get the next int but the input is invalid, it leaves the invalid input in the buffer when the exception is thrown. You need to add myscanner.next(); in the catch block so that the invalid input is flushed. So, the catch block will look like this:

...
catch (InputMismatchException e) {
    System.out.printf("Paycode cannot have decimal places. \n");
    myscanner.next();
}
...

Note that you do not need to print System.out.printf("Key in the paycode of the worker according to the worker type: \n"); in the catch block because that would make it print twice whenever there is invalid input.

As an aside, the semantics of your looping are a little confusing. I would set boolean good = false; to begin with and then set the while conditional to while (!good);. You will also need to change good = true; if the input is invalid. This makes it easier to understand why the program is looping (while the input is not good). Just a minor suggestion to make your code a little more readable.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! Now i know the wrong path that i did is i type paycode = myscanner.nextInt(); at last line in the catch which cause error in it.
If this answer was helpful to you, please accept it. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.