开发者

I am Facing issues in Scanner Class in java

开发者 https://www.devze.com 2023-04-12 01:05 出处:网络
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 i

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 (InputMis开发者_JAVA百科matchException 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();
   }
}


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");
   }
}


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 
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号