1

How to implement a data read from the keyboard without moving the cursor to the next line console using Java?

Specifically, the command line Windows.

java.util.Scanner moves the cursor to the next line its method, just like java.io.Console.

As far as I know, return the cursor to the previous line in the Java console can not be.

public static String Read() {
  Scanner Sc = new Scanner(System.in);
  return Sc.next();
}

System.out.print("Turn of player: "); 
TempPlayerGuess = Integer.parseInt(Read()); 
System.out.print(".");

And I see:

Turn of player: 4

.

But I want to see:

Turn of player: 4.

Destructor
  • 11
  • 2

1 Answers1

0

I don't think that's possible. System.in is an InputStream for accessing Standard Input. The input stream does not provide data until the user presses the return key (may be platform specific). Since all standard input uses System.in, I don't think it's possible to do what you're asking. Here's an example.

import java.io.IOException;

public class ReadInput {
    public static void main(String[] args) throws IOException {
        int readByte = -1;
        while ((readByte = System.in.read()) != -1) {
            System.out.println(String.format("Read character code %d", readByte));
        }
    }
}
Samuel
  • 16,923
  • 6
  • 62
  • 75
  • May it be somehow possible to convey character in InputStream to happen as I have shown above? @Samuel – Destructor Jul 21 '15 at 08:38
  • I don't have experience doing that, but there are supposedly libraries for it. See this question http://stackoverflow.com/questions/1001335/java-gotoxyx-y-for-console-applications – Samuel Jul 21 '15 at 13:00