Adrian
Adrian

Reputation: 20068

Is there a Java equivalent to getchar?

I have a simple client server program, wherein 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 the first time, but if I choose another option it doesn't print anything:

---- MENU ----

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

Enter a command: pwd
D:\Documents and Settings\asname\workspace_java\server

---- MENU ----

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

Enter a command: ls

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

Upvotes: 4

Views: 12043

Answers (1)

user467871
user467871

Reputation:

Just add scanner.nextLine(); after System.out.println(message); so it waits

Upvotes: 4

Related Questions