So in my computer science class I often finish assignments before the due date. In my free time I am creating a basic text rpg to entertain myself. One of the problems I am having that when I try to use the code response = in.nextLine(); I have to use that line of code twice to get the desired result.
Here is a method from my program.
public void room1(){
System.out.println("The room you are in is empty, I'm not sure how you got in here, I don't write stories. There is one exit to the North. What do you do?");
System.out.print(">");
response = in.nextLine();
response = in.nextLine();
response = response.toLowerCase();
if(response.indexOf("go north")>=0){
move();
move();
move();
room2();
}
}
as you can see I have two lines of repeating code, but in order for me to actually give an input like "Go North" the second line of code has to be there. Why is this the case and how can I make it so I don't have to repeat lines of code?
P.S. The move(); is because I am using Karel J Robot to function as a player and map for the game.
The answer is simple: java.util.Scanner's internal parser methods like nextInt(), nextDouble() and next() don't read parse newline characters.
Take for example, the following input...
5 4
three
At the beginning, before you have used your Scanner at all, your cursor is placed...
|5 4
three
...before the 5. Let's say you call in.nextInt()...
5| 4
three
Look at that! The cursor stops immediately after the 5 because that's where it has finished reading the integer. let's call in.nextInt() again...
5 4|
three
Now it is placed right after the 4. Notice that it isn't right before the 3... which is a huge difference. Because the difference between where it is now, and what the following snapshot portrays...
5 4
|three
...is HUGE. There is a newline character between those two cursor points. Now that's what your seemingly mysterious question pokes at... In your case, you probably run into this race condition where you used one of the parsing input methods from Scanner just prior to using nextLine() and left a latent newline somewhere, which your first call to in.nextLine() consumes, before it consumes the actual input.
So, if we go back to this state...
5 4|
three
Calling in.nextLine() will bring us to...
5 4
|three
(it consumed everything upto and including the newline, as was present in the buffer... which in this case results in an "" empty string).
Calling in.nextLine() one more time will bring us...
5 4
three
|
Notice how the cursor isn't at the end of the three, rather it is on the following line, because it consumed everything on the previous line, including the newline character that was present, which results in the string "three".
I loved playing around with this back in the day :). In fact, C++'s int five, four; cin >> five >> four; and string three; getline(cin, three); behave very similarly to this :), where you'd need an extra getline(cin, three); to get rid of that pesky trailing newline :)
The code you posted in itself doesn't contain any issues. I suspect you are reading something (such as an integer) elsewhere which leaves a newline in the input. So the when you call this method, that newline is consumed by the first nextLine() call. Hence, it appears to skip one input (or requiring you to have two nextLine() calls).
Consider this example:
import java.util.Scanner;
public class temp
{
public static Scanner in = new Scanner(System.in);
public static void main(String[] args)
{
while (true) {
System.out.print("Number:");
int n = in.nextInt();
System.out.print("Line1:");
String r1 = in.nextLine();
System.out.print("Line2:");
String r2 = in.nextLine();
System.out.println("Done "+r1);
System.out.println("Done "+r2);
}
}
}
This will always skip the "Line1" input. You probably have the exactly the same problem. You can fix it by having a call to nextLine() where you might leave a trailing newline.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With