Error when taking the letter s
by Scanner
:
do{
//¿Volver a jugar?
Scanner teclado = new Scanner(System.in);
System.out.println("¿Volver a jugar?(s/n): ");
volverjugar = teclado.nextLine();
System.out.println(volverjugar);
} while ( volverjugar != "s" );
The error is due to the fact that you never declared the identifier
volverjugador
, that is, the compiler will never recognize it and for that reason you get that compilation error.One possible solution is to declare the variable
volverjugar
(it should be of typestring
) as local at the beginning of the methodmain
.That way, the compiler shouldn't send any syntax errors; however, your code has several flaws:
1. In this part of the code a possible infinite loop will occur:
If you notice, the code above will keep asking the user for information until they enter the correct number. The problem is that we do not know when the number entered by the user will coincide with the value that
rand
. It could be in 10 tries, in 1000 tries. To avoid this inconvenience, we could assign a number entry limit (for example,5
attempts), in this way, we make the game more interactive with the user.In code it would look like this:
2. In this part of the code you are comparing references:
What the code above will do is compare references to objects, that is, an infinite loop will occur, because the references (memory addresses) will not be equal.
One possible solution is to use the equalsIgnoreCase method of the class
String
.So with this method we can safely compare strings, if the user enters
SI
orsi
, the method will returntrue
and make the game start again. If the user enters thatNO
orno
, it will returnfalse
and this will cause the loop to end (because the conditional will not be fulfilled and therefore the loop breaks).3. It is not necessary to create objects of type in each iteration
Scanner
.By creating objects of type
Scanner
, the only thing you will achieve is to give more processes to the Java garbage collector , because, in each repetition, it must reserve memory dynamically for an object and in the process, look for the precise moment to free memory.One way to optimize this is by saving the type object reference
Scanner
at the beginning of the methodmain
.The complete code, with the changes made, would look like this:
This line of code:
All it does is clear the keyboard buffer. This is necessary because as the method
nextInt
(of the classScanner
) was called, it will leave the dirty buffer (with the line break) and this must be cleared, otherwise, when you want to ask the user if he wants to return to play or no, it won't pause the program and since itvolverjugar
will be worth\n
, it will make the conditional:volverjugar.equalsIgnoreCase("si") == true
result infalse
, causing the game to end without the user having made the decision to exit.Assuming the variable is of type Char, this would work for you:
this may help you
Is it possible to read a character by keyboard in Java?
To fetch a String with Scanner you must use nextLine() method .
Also when you ask for console System.out.print ln ("Play again? (y/n):") note that it is with ln so that it gives a line break and takes the input keyboard.nextLine() . So:
I must point out that the method, as much as it works for you, is not done like this. First of all, the Scanner is not initialized inside the method since each time it is called, a new Scanner object will be created. After which must be used inside a loop so that the play action is repeated as long as returnplay is worth s