Is there a Java equivalent for getchar?

I have a simple client server program in which I have a problem with my menu:

do { System.out.println("---- MENU ----\n"); System.out.println("(drv) - List drives"); System.out.println("(pwd) - Current path"); System.out.println("(ls) - List files in a directory"); System.out.println("(quit) - Quit program\n"); System.out.print("Enter a command: "); command = scanner.nextLine(); sendMessage(command); message = (String)in.readObject(); System.out.println(message); System.in.read(); }while(!command.equals("quit"));` 

I want to print the result on the screen, wait until I press Enter , and then show the menu again, so I use System.in.read() . My problem is that it works for the first time, but if I choose another option, it doesn't print anything:

---- MENU ----

(drv) - List of drives
(pwd) - Current path
(ls) - List of files in a directory
(quit) - Close the program

Enter the command: pwd
D: \ Documents and Settings \ username \ workspace_java \ server

---- MENU ----

(drv) - List of drives
(pwd) - Current path
(ls) - List of files in a directory
(quit) - Close the program

Enter the command: ls

Is there a method in Java similar to getchar that I could use instead?

+4
source share
1 answer

Just add scanner.nextLine(); after System.out.println(message); so he waited

+4
source

Source: https://habr.com/ru/post/1337518/


All Articles