Using the Java Scanner

20

I was doing an exercise for the university using the class Scanner and something unusual happened, watch 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(P[i]);
  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 the responses of both players, but check out the program:

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

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

As a workaround I created another class Scanner for resposta1 and resposta2 separating the readers, which in the end worked. But I was curious as to why the error reader being used multiple times

    
asked by anonymous 28.08.2014 / 13:40

2 answers

15

It's because of the ENTER you get when you read the integer.

UPDATE: I was wrong, I changed nextLine() to readLine() . It's already fixed.

The ENTER produces a string \n that is consumed by the next nextLine() command. You would have to put another nextLine() to read this \n , that is, two nextLine() followed, one to consume \n and another to read the user response.

    
28.08.2014 / 14:41
17

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

Explaining the error with my own words, when you use the command nextInt(); you are reading the next integer, however you are not reading the entire line, and on the whole line there is something more than just the integer you entered .

Make the following test that will be clearer for you, instead of typing 10 , ENTER , 10 , ENTER , do so, 10 10 D , ENTER , D with%. That is, enter twice the 10 and already enter the response of player 1, as soon as the program is waiting for the first entry. The program will read the first 10 and assign it to ENTER because of aposta1 , read the second 10 and assign it to nextInt() because of the other aposta2 , read the rest of the line because of the nextInt() and will assign nextLine() and then wait for the resposta1 entry, which will also be resposta2 .

So that would be one of the correct means of entering the data in your program the way it is, though we know not that this is what you want, the correct thing would be to correct your program so that it becomes more intuitive for the user.

One possible solution:

Do not use D , use nextInt() , make sure the input is an integer and then convert it to int with the command leitor.nextLine();

In addition, you will be making your program resistant to invalid entries when prompted to enter an integer, because the way it is if you type a character in place of an integer an exception will be thrown in the user's face, and the ideal would you treat it and present a more user-friendly error message, than a bunch of incomprehensible code for non-programmers.

    
28.08.2014 / 15:04