Nithish
Nithish

Reputation: 103

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

Upvotes: 0

Views: 169

Answers (4)

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

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

Upvotes: 0

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26737

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

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

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

Upvotes: 2

Mark Rotteveel
Mark Rotteveel

Reputation: 109014

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.

Upvotes: 4

Related Questions