0

I really have no idea about this problem...

The block catches an exception if the number is not correct, when I put -1 or 0 it catches the Exception and asks me to input the number again... but if I type something like asdasd it will run an infinite loop.

while (true){

            try{

                System.out.println("-Size of the array: ");        
                size = read.nextInt();      

                if(size<=0){

                    throw new Exception();

                }else{

                    break;    

                }


            }            
            catch(Exception e){

                System.out.println("\n-Wrong input. Try again.\n");

            }    

        }
OHHH
  • 1,011
  • 3
  • 16
  • 34
  • 1
    http://stackoverflow.com/questions/5909518/java-nextint-error – lc. Oct 15 '12 at 05:17
  • lc's comment is a good one, you should check it out. Also, you might try calling `reset` on `read` (which I assume is a `Scanner`) in the `catch` section... – MadProgrammer Oct 15 '12 at 05:19

4 Answers4

2

Probably the best way to deal with this is to change it so that reader gets the next line:

String input = read.nextLine();
if(input.length() == 0) { continue; }

try{
    size = Integer.parseInt(input);
} catch(NumberFormatException e){ throw new Exception(); }
2

Put the Scanner initialization inside while loop:

while (true){

        try{
            Scanner read = new Scanner(System.in);
            System.out.println("-Size of the array: ");        
            size = read.nextInt();      

            if(size<=0){

                throw new Exception();

            }else{

                break;    

            }


        }            
        catch(Exception e){

            System.out.println("\n-Wrong input. Try again.\n");

        }    

    }
null
  • 8,669
  • 16
  • 68
  • 98
  • Can you tell me why we need to call scanner inside loop? I have called in on top of my code and then it gave infinite loop – Umair Hamid Apr 06 '15 at 16:49
  • 1
    @UmairHamid Maybe this is the answer: http://stackoverflow.com/questions/10468455/simple-java-scanner-code-not-working. The `nextInt` doesn't consume the line-feed, so the next call of nextInt (if you don't reset the scanner) will cause the nextInt executed directly, because there is already a line-feed in the input buffer. Beside using `new Scanner(System.in)`, I think you can use `Scanner.reset` method too to prevent this. – null Apr 20 '15 at 09:11
0

My guess is that read.nextInt is throwing an exception because "asdasd" is not an integer. This will cause your exception handler to be run and print the message. However, because you don't have "break" in your exception handler, the loop will be run again... and again...

Peter
  • 669
  • 5
  • 14
0

I suspect that you are getting caught out because you are throwing and catching Exception. If you add e.printStackTrace(); to the catch plock that you will probably find that you are catching an exception that you are not expecting to happen ... like one coming from the nextInt() call, or an NPE or something.

  • Don't throw Exception. Not ever. Use a specific exception, creating one for your self if none of the standard ones is appropriate.

  • Don't catch Exception unless you are prepared to deal with ALL of the possibilities. And that includes all of those unexpected unchecked exceptions that are due to random bugs in your code.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216