0

I encountered a problem with my for loop.

My objective is to

  1. Check that integers are entered instead of strings by user.
  2. Loop it 6 times, each time prompting the user for input.

With my current code, I am getting this output:

Enter integer: 
a
invalid input
Enter integer: 
invalid input
Enter integer: 
invalid input
Enter integer: 
invalid input
Enter integer: 
invalid input
Enter integer: 
invalid input
BUILD SUCCESSFUL (total time: 3 seconds)

I am able to check the user input for integer. However, my for loop doesn't seem to be working correctly. May I request assistance in this?

Below is my code:

String a = "";
int count;

for (count = 0; count < 6; count++) {
    System.out.println("Enter integer: ");

    if (keyboard.hasNextInt()) {  
        System.out.println(a + "is correct integer"); 
    }
    else {
        System.out.println("invalid input");
    }
}
Tom
  • 16,842
  • 17
  • 45
  • 54
gymcode
  • 4,431
  • 15
  • 72
  • 128

6 Answers6

4

You aren't consuming any of the user input and a should be an int if you're going to read int(s). I think you wanted something like

for (int count = 0; count < 6; count++) {
    System.out.println("Enter integer: ");
    if (keyboard.hasNextInt()) {
        int a = keyboard.nextInt();
        System.out.printf("%d is an integer%n", a);
    } else {
        System.out.println("invalid input " + keyboard.nextLine());
    }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I guess you should change `nextLine` to `next`, because `nextLine` would first read the remainin line feed and on the next iteration the invalid input. This "wastes" one interation. – Tom Jan 05 '15 at 13:22
2

Try this:

it consumes the wrong line and as you again.

                String a = "";
    int count;
    Scanner keyboard = new Scanner(System.in);
    for (count = 0; count < 6; count++) {
        System.out.println("Enter integer: ");

        if (keyboard.hasNextInt()) {
            a = String.valueOf(keyboard.nextInt());
            System.out.println(a + "is correct integer");
        } else {
            System.out.println("invalid input");
            keyboard.nextLine();
        }
    }
Jens
  • 67,715
  • 15
  • 98
  • 113
  • Thank you Jens. I am now able to prompt user for another input if he enters a Char. However, when he enters an Int, the loop still malfunctioned. It gives the same output of not prompting the user for another input. – gymcode Jan 05 '15 at 12:08
  • @RUiHAO: I have changed my answer. Please try again. – Jens Jan 05 '15 at 12:10
  • I guess you should change `nextLine` to `next`, because `nextLine` would first read the remainin line feed and on the next iteration the invalid input. This "wastes" one interation. – Tom Jan 05 '15 at 13:23
2

You must consume the keyboard input, else it will still be there in subsequent loop iterations.

if(keyboard.hasNextInt())
{
  // consume the input by calling nextInt()
  System.out.println(keyboard.nextInt() + " is correct integer"); 
}
else
{
  // consume the input by calling next()
  System.out.println("invalid input " + keyboard.next());
}
gknicker
  • 5,509
  • 2
  • 25
  • 41
1

Your code needs following changes:

  1. Assign String a with input value:

    if (keyboard.hasNextInt()) {
        a = keyboard.next();
        System.out.println(a + "is correct integer");
    } else {
        a = keyboard.next();
        System.out.println("invalid input");
    }
    
  2. initialize keyboard variable properly:

    Scanner keyboard = new Scanner(System.in);
    

This question might be helpful Java: using hasNextInt with do-while loop, it ignores integer inputs at even times.

Community
  • 1
  • 1
100rabh
  • 142
  • 9
  • This worked for me. I am very puzzled. In the other codes, I will meet an InputMismatchException when I enter an incorrect input after the previous correct input (entering wrong input after a correct input). However, in this code, the error did not appear. May I know if anyone know the reason why? – gymcode Jan 05 '15 at 13:04
  • @RUiHAO You haven't mentioned 'Other codes' but In my opinion you were getting InputMismatchException because you were consuming 'incorrect input'(a string input) with keyboard.nextInt(); Now in my code above, we are consuming every input with keyboard.next(); which works fine with int/string inputs. Please check following links for more details:: http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html http://docs.oracle.com/javase/7/docs/api/java/util/InputMismatchException.html – 100rabh Jan 07 '15 at 07:59
0
    for (int i = 0; i < 6; i++){
        Scanner input = new Scanner(System.in);
        System.out.println("Enter integer:");
        char value = input.nextLine().charAt(0);
        if(Character.isDigit(value)){
            System.out.println(value + " is correct integer"); 
        }else{
            System.out.println(value + " invalid input");
        }

    }
JustStarted
  • 199
  • 4
  • 13
-3

You need to Scan the Value from system therefore use Import scanner

**

USE this code

**

Scanner s = new Scanner(System.in);
        int i;
        String a = "";
        int count;

        for (count = 0; count<6; count++)
                {
            System.out.println("Enter integer: ");

            if(s.hasNextInt()) 
                        {  
                        System.out.println(a + "is correct integer"); 
                        }
                        else
                        {
                        System.out.println("invalid input");
                        }
                } 
Dilip
  • 2,622
  • 1
  • 20
  • 27