I have 2 threads, one that waits for user input with nextLine, the other that writes to stdout. This is a demo example:
package com.ricvail.example;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class App {
public static void main( String[] args )
{
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
System.out.print("This will interrupt the user while he's typing");
}
}, 5000, 5000);
boolean exitLoop=false;
Scanner stdin = new Scanner(System.in);
while (!exitLoop){
String inputLine = stdin.nextLine();
System.out.println("Echoing: "+ inputLine);
}
}
public int asd(){
return 3;
}
}
How can I modify the println()
and nextLine()
statements so that the first does not interfere with the second?
I mean, right now if I try to write "Hello, I'm typing something" that's what happens:
Hello, I'm tyThis will interrupt the user while he's typingping something
Echoing: ping something
This will interrupt the user while he's typing
I would like to be able to read what the user has been typing so far, erase his line, replace it with the println, and then write back the user input on stdIn. I think curses might help me but I'm not allowed to use external libraries, is there a way to do it with plain java?