-1
package exercises;
import java.util.*;

public class Try_and_catch {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int x=1;
        do
        {   
            System.out.println("Enter first number");
            int n1 = input.nextInt();
            System.out.println("Enter second number");
            int n2 = input.nextInt();
            int sum= n1/n2;
            System.out.println(sum);
        } while(x==1);
    }
}

The code above requires input only integers, my question is how to handle the error whenever the user input a character?

bummi
  • 27,123
  • 14
  • 62
  • 101
  • if u enter valid integers then there should be no input mismatch exception. make sure that you enter the two integers in the same line separated by a space. – Juned Ahsan Oct 12 '14 at 03:57
  • The code above requires input only integers,,, my question is how to handle the error whenever the user input a characater?....thak you – Sandy Rosas Oct 12 '14 at 03:58
  • if the user enter a character the error pops up... i want it to handle the error – Sandy Rosas Oct 12 '14 at 03:59

1 Answers1

0

Use a try block:

boolean again = true;
int n1;
while (again) {
  try {
    System.out.println("Enter first number");
    input.nextInt();
    again=false;
  }
  catch(InputMismatchException ime)
  {
    // do nothing!
  }
}

What happens here is pretty simple: if we get an exception, then "again" is not set to true and we go back around in the loop. If we get out of the try block without an exception, then again is toggled and we go merrily on our way.

Jon Kiparsky
  • 7,499
  • 2
  • 23
  • 38
  • Thank you for the answer... it works.... but when i run it and hit any character the "Enter first nymber" keeps on displaying repeatedly.... this is my next question... thank you so much... – Sandy Rosas Oct 12 '14 at 04:10
  • Ah, sorry, this is a weird glitch in the Scanner. Add a call to input.nextLine() in the catch block. This will eat the newline that nextInt() left in the pipe. Should have thought of that, my bad. – Jon Kiparsky Oct 12 '14 at 04:15
  • 1
    It would be better if you wrap this code inside a method and use that to get each number instead of duplicating code. – dramzy Oct 12 '14 at 04:18
  • to Jon Kiparsky..... i really appreciate your answer ... it really helps a lot... now i have answer on our defend java program... thank you... my question is answered – Sandy Rosas Oct 12 '14 at 04:21
  • may i ask another question? how should i'm going to make my password converted into asterisk? – Sandy Rosas Oct 12 '14 at 04:23
  • @dramzy I agree, but I prefer to present the minimal solution to the problem at hand, since that's a clearer answer. Discussions of best practice distract from the questioner's current concern. (there are other sites, like dreamincode.net, for that) – Jon Kiparsky Oct 12 '14 at 04:30
  • @SandyRosas I think that's a new question, and I'm not going to address it here in the comments. Fortunately, you're allowed to post a new question! :) – Jon Kiparsky Oct 12 '14 at 04:31
  • Yeah, I was just making a suggestion to the OP. – dramzy Oct 12 '14 at 04:31
  • ok2x... thank you so much... i'm gonna make another question for it...thnx again – Sandy Rosas Oct 12 '14 at 04:33