How to use the Java scanner

Asked

Viewed 1,978 times

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 answers of 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:
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’s resposta1 not allowing me to put a reply, returning as empty. This kind of problem only happened after I entered the readers of the aposta1 and aposta2, when they are removed the readers of the resposta1 and resposta2 are read normally.

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

  • From the method’s use description, it seemed to me that it advances a line of code and stops at the next use of the Scanner. That would be my explanation, I don’t know if you’re right.Try to use it. leitor.next();, at least it worked for me.

2 answers

17

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

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

Do the following test that will become clearer to you, instead of typing 10, ENTER, 10, ENTER, do so, 10 10 D, ENTER, D, ENTER. That is, type twice the 10 and already type the answer of player 1, so the program is waiting for the first entry. The program will read the first 10 and assign it to aposta1 because of the nextInt(), read the second 10 and attribute it to the aposta2 because of the other nextInt(), will read the rest of the line because of the command nextLine() and will assign the resposta1 and then wait for the entrance of resposta2, which will also be D.

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

A possible solution:

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

Even, so you will be making your program resistant to invalid entries when asked to enter a whole, because the way it is if you type a character in place of a whole one exception will be thrown in the user’s face, and the ideal would be you treat itthere and present a more user friendly error message, than a lot of incomprehensible code for non programmers.

  • I also understood this with the method leitor.next(). If it is String, for example, it will fill with up to the first "space"

  • Opa vlw for explanation and help!

15


Is because of the ENTER that you give when you read the whole.

UPDATE: I made a mistake, changed nextLine() for readLine(). Is already corrected.

The ENTER produces a string \n which is consumed by the next command nextLine(). You’d have to add one more nextLine() to read this \n, ie two nextLine() followed, one to consume the \n and another to read the user’s response.

  • 1

    That’s right Earendul, but to use nextLine(); for the second answer, it should be used only once.

  • Thank you both very much, both solutions have solved the problem. I thank you for your time the/ . I’d like to vote positive, but I don’t have enough reputation.

  • Raul, it’s not over yet :) Give the question as closed giving the best answer to Earendul’s answer, since I only commented on your question

  • For this just click the button below the vote? I’m newcomer xD

Browser other questions tagged

You are not signed in. Login or sign up in order to post.