简体   繁体   中英

I am Facing issues in Scanner Class in java

If I enter the wrong input(example , if I enter String instead of Integer) loop is not ending, it wont get input next time. Here(below) i attach the entire program. can you please help this?. Thanks in advance!!!

import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * If we enter the wrong input(example , if we enter sting instead of integer) it goes unending loop
 * 
 * @author Nithish
 *
 */
public class Sample2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < 1; i++) {
            try {
                System.out.println("Enter the value");
                int obj = scanner.nextInt();
                System.out.println(obj);
            } catch (InputMismatchException e) {
                i--;
                e.printStackTrace();
            }
        }
    }
}

On an InputMismatchException you are doing i--, so the loop condition is modified to prevent the loop from ending without the needed input. If you read the API documentation for Scanner.nextInt() you should notice the following:

If the translation is successful, the scanner advances past the input that matched.

This means that if the input cannot be translated to int, the scanner does not advance. So on the next invocation of nextInt() it will re-read the exact same, non-int, input and fail again. You will need to read past that non-integer token before attempting to get an int again.

Again, don't mess with the loop index inside of the loop as this can cause problems down the road. Instead use a while loop which is much cleaner and much easier to debug 3 months from now:

import java.util.InputMismatchException;
import java.util.Scanner;

public class Sample2 {
   public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      boolean done = false;
      int result = 0;

      while (!done) {
         try {
            System.out.print("Enter the value: ");
            String temp = scanner.nextLine();
            result = Integer.parseInt(temp);
            done = true;
         } catch (NumberFormatException e) {
            System.out.println("Please only enter integer data");
         }
      }
      scanner.close();
   }
}
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
    try {
        System.out.println("Enter the value");
        int obj = scanner.nextInt();
        System.out.println(obj);
    } catch (InputMismatchException e) {
        i--;
        //e.printStackTrace();
        scanner.nextLine(); //you can add this here.
        //scanner.next(); you can also use this 
    }
}

what about the below?

Scanner sc = new Scanner(System.in);
while (!sc.hasNext()) {
  System.out.println("Enter the value");
   if (src.hasNextInt()) {
      i = src.nextInt();
      System.out.println("Thank you! (" + i+ ")");
   }
      else
   {
      System.out.println("Please only int");
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM