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

6 Answers 6

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());
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

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

3 Comments

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.
@RUiHAO: I have changed my answer. Please try again.
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.
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.

2 Comments

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?
@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:: docs.oracle.com/javase/7/docs/api/java/util/Scanner.html docs.oracle.com/javase/7/docs/api/java/util/…
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");
        }

    }

1 Comment

"-1" is not a valid integer? That's sad :(.
-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");
                        }
                } 

1 Comment

The keyboard variable in his code example is already a Scanner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.