2

i have a program that I am trying to add exception handling to. The problem is, the exception that i wrote up still exits the program. Basically i offer the user to enter in any int, if they throw me a char, the exception says that they cant do that and lets them enter another int. But it isn't working giving me this error:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at cit130_hw10_q3.Cit130_hw10_q3.main(Cit130_hw10_q3.java:29)

Java Result: 1

Here's some code. Thanks for any help you might offer.

   Scanner input = new Scanner(System.in);
   System.out.println("Enter a series of integers." +
           "When finished enter 999");
   int userInput = 0;
   int inputCount = 0;
   do{
       try{
           System.out.println("Enter an integer: ");
           userInput = input.nextInt();
           addToArray(userInput, inputCount);
       }
       catch(Exception e){
           System.out.println("Only integer values are accepted. Please try again");

       }
       inputCount++;
   }while (userInput != 999);

public static void addToArray(int nextInt , int inputCount){
    integerArray[inputCount] = nextInt;
}
12
  • 2
    Clear the project, recompile it and retry. Also ensure Exception is java.lang.Exception not something else Commented Dec 10, 2013 at 6:26
  • 1
    Your addToArray function might throw some more light on the problem you are facing.. Please show that code.. Commented Dec 10, 2013 at 6:26
  • I guess you mean while (inputCount != 999) ? Commented Dec 10, 2013 at 6:30
  • @VikasV that would show up in the stack trace though. The stack trace is indicating a problem in main, not in a function. Commented Dec 10, 2013 at 6:32
  • 2
    Also, note that if nextInt throws InputMismatchException, it will not advance the scanner. Calling nextInt again will just try to re-read the same non-int again. You need to invoke next() in the catch block to advance the token. With that additional tweak, your code works fine on my computer. I think RC has it right. Commented Dec 10, 2013 at 6:33

2 Answers 2

2

I suspect you forgot to recompile in-between or are running some other version of the code, maybe a different file than this. If you have java.lang.Exception catched like you show, this will ot happen.

Other option is that you have input.nextInt(); somewhere else, too, in a part that you did not paste here. Reconfirm line 29.

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

Comments

0

Excpetion class is the base class for all exceptions and if you are unsure of exception type you can catch it through java.lang.Excpetion and this works perfectly. In your case please put a break point in the try catch block and then confirm the point where exception is thrown. This will give an insight as why it is not getting catched at the place you mentioned

2 Comments

That would be better posted as a comment
Ya you are correct as per stack overflow guidelines this was suppose to be a comment

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.