I would like assign a time limit to an input so that if the user does not insert an input within the time limit the input will be cancelled
how do i do that?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I would like assign a time limit to an input so that if the user does not insert an input within the time limit the input will be cancelled
how do i do that?
From where? The console? A GUI textbox?
the console
hmmm, that's tricky...
I came up with a rudimentary solution which reads in from System.in in a separate thread and puts it into a StringBuilder buffer which can then be read externally. While someone else isn't actively trying to read from the reader, the buffer is emptied. Note that there needs to be synchronization between the two threads on the buffer otherwise bad things will happen.
This may be good enough to work with most user inputs, but you may want to improve on it. There are still a few issues (both those I have thought of and those I didn't think about).
import java.util.Scanner; public class TimedScanner implements Runnable { public static void main(String[] args) { TimedScanner scanner = new TimedScanner(); System.out.print("Enter your name in 10 second: "); String name = scanner.nextLine(10000); if (name == null) { System.out.println("you were too slow."); } else { System.out.println("Hello, " + name); } } private Scanner scanner; private StringBuilder buffer; private boolean reading; private Thread t; public TimedScanner() { scanner = new Scanner(System.in); buffer = new StringBuilder(); reading = false; t = new Thread(this); t.setDaemon(true); t.start(); } public String nextLine(long time) { reading = true; String result = null; long startTime = System.currentTimeMillis(); while (System.currentTimeMillis() - startTime < time && result == null) { try { Thread.sleep(30); } catch (InterruptedException e) { } synchronized (buffer) { if (buffer.length() > 0) { Scanner temp = new Scanner(buffer.toString()); result = temp.nextLine(); } } } reading = false; return result; } @Override public void run() { while (scanner.hasNextLine()) { String line = scanner.nextLine(); synchronized (buffer) { if (reading) { buffer.append(line); buffer.append("\n"); } else { // flush the buffer if (buffer.length() != 0) { buffer.delete(0, buffer.length()); } } } } } }
isn't reading from InputStream e.g. System.in is a blocking call ?
Yes it is, which is what makes this task tricky. The code I had puts the blocking call in a separate thread and passes the data to a buffer, but it's hardly ideal.
I dont understand what are the downsides in this methode