I've just started working with Java, and I've been trying to get the console input to work properly. Here's the code:
System.out.println("Write a word: ");
Scanner keyboard = new Scanner(System.in);
System.out.println("DEBUG 1");
str = keyboard.nextLine();
System.out.println("DEBUG 2");
System.out.println(str);
This should just take input once and the print the input, like this:
Write a word:
DEBUG 1
Hello //My input
DEBUG 2
Hello //Output
But this is what happens:
Write a word:
Hello //My input
DEBUG 1
//Waiting for new input
DEBUG 2
Hello //The first input
So, it seems to me that it's somehow takes the input at the line Scanner keyboard = new Scanner(System.in); and then put it in my variable str. I use gcj to compile with the following commands to compile and run:
javac hello_world.java
java hello_world
EDIT: I've tried running the program on another computer now, using Ubuntu 10.04 64-bit, instead of Ubuntu 10.04 32-bit as before. I ran it the same way and did not recompile, and the program works fine.
Any ideas why it's behaving like this?