3

I know this might sound like a really stupid question but I cannot understand where is my mistake.

Why on the second iteration of the loop, it does not print 'Enter a number:'?

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner console = new Scanner(System.in);
        int[] v = new int[10];
        int index = 0;
        do {
            System.out.print("Enter a number:\t");
            v[index] = console.nextInt();
            index++;
        } while(console.hasNextInt());

        for (int i = 0; i < index; i++){
            System.out.print(v[i] + "\t");
        }
        System.out.println("\n" + index);
    }
}

And this is the output:

Enter a number: 1
2
Enter a number: 3
Enter a number: 4
Enter a number: 5
Enter a number: ^D
1       2       3       4       5
5
3
  • Are you sure that you want to use a do-while loop, instead of a while loop? Commented May 3, 2019 at 12:22
  • @P.Soutzikevich if you just substituted a while for the do...while they would not see the first "Entere a number" instead. Commented May 3, 2019 at 12:44
  • @FedericoklezCulloca oh I thought the OP had that outside of the loop block too. my bad Commented May 3, 2019 at 15:23

3 Answers 3

3

Because hasNextInt blocks until there is an int on the console, therefore not entering the next iteration of the loop.

Sign up to request clarification or add additional context in comments.

Comments

1

Welcome. See answer in comments. Hope it is clear :-)

do {
        System.out.print("Enter a number:\t");  // Prints "Enter a number: "
        v[index] = console.nextInt(); // accepts input "1"
        index++; // increments
    } while(console.hasNextInt()); // waits for input at which point you enter "2"

Ok Let's make it clearer.

The do while loop, executes the do block before it evaluates the while condition. And then if the while condition evaluates to true, it executes the do block again and repeats until the while condition evaluates to false. .

Both console.nextInt and console.hasNextInt read input from the console. So as part of the do block, the "Enter a number:\t" has been printed out, The first nextInt() call has accepted the input "1", which is then followed by the increment, followed by evaluation of the while condition - the console.hasNextInt(), which again waits for input and accepts "2". This explains why the "Enter a number:\t" was not printed before the User Input of "2"

Of course because the value 2 has been entered, the while condition evaluates to true and again the do block is executed and goes on.

Perhaps you need the while loop. This, by contrast, executes the code block only if and as long as the while condition evaluates to true

System.out.print("Enter a number:\t");
while(console.hasNextInt()){
        v[index] = console.nextInt(); 
        index++; // increments
        System.out.print("Enter a number:\t");  
    } 

3 Comments

This is exactly the same as OP's code, except that you've added comments (unless I'm skipping over a change you've did by accident).. How does it help understanding the incorrect output?
That's indeed a lot better, +1 from me :)
@KevinCruijssen Thanks for your comment. Happy to improve :-)
0

Think of exiting the loop when you reached the enough size of the array. something like

   do {
            System.out.print("Enter a number:\t");
            v[index] = console.nextInt();
            index++;
        } while(index<10); // if you want the user to enter 10 numbers. 

Comments

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.