Reputation: 35
public static void main(String[] args)
{
String word;
System.out.println ("Please type hello");
Scanner scanner = new Scanner(System.in);
word = scanner.nextLine();
System.out.println ("Okay");
}
I have a problem with nextLine() method and it is that when I type a word on a keyboard, I want the cursor of the keyboard to be on the next line of "Okay". However, it is on the same line as "Okay".
Can you please let me know how I can move the cursor to the next line it as I've been on this for 2 hours and couldn't find any solutions?
Thanks in advance.
I tried to find solutions for this problem but all of them are related to nextInt(), which I haven't used in this code. Also I tried to use "\n" but it didn't solve anything.
Upvotes: 0
Views: 1457
Reputation: 2587
I suspect you are using the simulated console in your IDE (Eclipse or something) to test-run your program, right? Is this what happens?
The simple answer is: This doesn't work like a real terminal. While you can just type even if the cursor is in the "wrong" line and the input text will appear in the "correct" line, it doesn't create a perfect illusion of a proper terminal.
Export your program to a runnable .jar
file and run it on your systems terminal/console with java -jar yourProgramName.jar
and you will be happy!
Upvotes: 3
Reputation: 6693
Did you missed a System.out.println
?
public static void main(String[] args)
{
String word;
System.out.println ("Please type hello");
Scanner scanner = new Scanner(System.in);
word = scanner.nextLine();
System.out.println (word); // << missed line
System.out.println ("Okay");
}
Upvotes: 1