How to use Java scanner


I was doing an exercise for the university using the Scanner class and something unusual happened, look at the code.

for(int i = 0; i < 11; i ++){
  //problema(esta pulando a escolha de um dos jogadores, precisamente o jogador 1)
  System.out.println("Jogador 1, faça a sua aposta:");
  aposta1 = leitor.nextInt();
  System.out.println("Jogador 2, faça a sua aposta:");
  aposta2 = leitor.nextInt();   
  System.out.println("Jogador 1, insira a sua resposta:");
  resposta1 = leitor.nextLine();  
  System.out.println("Jogador 2, insira a sua resposta:");
  resposta2 = leitor.nextLine();

This code should accept responses from both players, but see the output of the program:

Quanto é 2 + 2?
A- 1
B- 2
C- 3
D- 4
Jogador 1, faça a sua aposta:
Jogador 2, faça a sua aposta:
Jogador 1, insira a sua resposta:
Jogador 2, insira a sua resposta:
O jogador 1 errou
O Jogador 2 acertou

As you can see, the program skips the resposta1 reader1 not allowing me to put an answer, returning as empty. This type of problem only happened after I insert readers aposta1 and aposta2 when they are removed from the readers of resposta1 and resposta2 are read normally.

As a solution I created another class Scanner for the resposta1 and resposta2 separating the readers, which in the end worked. But I was curious as to why the reader gets it wrong when used multiple times.


@Earendul's answer already points out the correct reason why the error occurs, however there is another solution to what he proposes.

Explaining the error in my own words, when you use the nextInt(); command nextInt(); you are reading the next integer, however you are not reading the entire line, and in the entire line there is more to it than just the integer you typed.

Do the following test that will be clearer to you, instead of typing 10 , ENTER , 10 , ENTER , do it like this, 10 10 D , ENTER , D , ENTER . In other words, type the 10 twice and already type the answer from player 1, as soon as the program is waiting for the first input. The program will read the first 10 and assign it to aposta1 because of the nextInt() , it will read the second 10 and assign it to aposta2 because of the other nextInt() , it will read the rest of the line because of the nextLine() command and assign resposta1 and then wait for the input of resposta2 , which will also be D .

So it would be one of the correct ways to enter the data in your program the way it is, however we know that this is not what you want, the correct one would be to correct your program so that it is more intuitive for the user.

A possible solution:

Don't use nextInt() , use leitor.nextLine(); , check if the input is an integer and then convert it to int with the command Integer.parseInt()

Also, this way you will be making your program resistant to invalid inputs when you are asked to enter an integer, because the way it is if you type a character instead of an integer an exception will be thrown in the users face, and ideally you would treat it and present a friendlier error message than a bunch of code incomprehensible to non-programmers.

Scroll to Top