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.

Results 1 to 7 of 7

Thread: How do i limit the input time

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How do i limit the input time

    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?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How do i limit the input time

    From where? The console? A GUI textbox?

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How do i limit the input time

    the console

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How do i limit the input time

    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());
    					}
    				}
    			}
    		}
    	}
    }

  5. The Following 2 Users Say Thank You to helloworld922 For This Useful Post:

    itayj (October 24th, 2011), tim91700 (November 11th, 2012)

  6. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do i limit the input time

    isn't reading from InputStream e.g. System.in is a blocking call ?

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How do i limit the input time

    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.

  8. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How do i limit the input time

    I dont understand what are the downsides in this methode

Similar Threads

  1. file upload size limit
    By shiv@P in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: December 24th, 2012, 11:50 PM
  2. set time limit on running of a thread
    By z.zojaji in forum Threads
    Replies: 3
    Last Post: July 10th, 2010, 10:57 AM
  3. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 04:28 PM
  4. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM