Time Limit for an Input

Time limit for an input

import java.util.Timer;
import java.util.TimerTask;
import java.io.*;
public class test
{
private String str = "";

TimerTask task = new TimerTask()
{
public void run()
{
if( str.equals("") )
{
System.out.println( "you input nothing. exit..." );
System.exit( 0 );
}
}
};

public void getInput() throws Exception
{
Timer timer = new Timer();
timer.schedule( task, 10*1000 );

System.out.println( "Input a string within 10 seconds: " );
BufferedReader in = new BufferedReader(
new InputStreamReader( System.in ) );
str = in.readLine();

timer.cancel();
System.out.println( "you have entered: "+ str );
}

public static void main( String[] args )
{
try
{
(new test()).getInput();
}
catch( Exception e )
{
System.out.println( e );
}
System.out.println( "main exit..." );
}
}

how to take steps and limits to input time in html5?

You need to use below attributes :

  • step for the steps
  • min for the lower limits
  • max for upper limits

Check the below code :

<input type="time" step="600" min="07:00" max="23:00" />

Limit User Input of HTML input type = time After Current Time

You can use date to format timestamps. If no timestamp is provided to date, it defaults to the current timestamp.

<?php $currenttime = date("H:i"); ?>
<input type="time" name="time" min="<?php echo $currenttime?>">

Time limit for an input inside a loop

This can be achieved with an event based model. You will need two threads for this (one of these can be the main thread).

The first thread will accept input from a Scanner and add it to an event queue (in our case, an "event" is simply the string that gets entered):

private BlockingQueue<String> lines = new LinkedBlockingQueue<>();

Start said thread in your main method:

Thread t = new Thread(() -> {
Scanner s = new Scanner(System.in);
while (true) {
lines.add(s.nextLine());
}
});
t.setDaemon(true); // so that this thread doesn't hang at the end of your program
t.start();

Then, when you want to get the input, you can get it from the queue with a timeout:

return lines.poll(5, TimeUnit.SECONDS);

Input Time Limit

import time at the start of it, then:

...
if whichtasktodo == "Empty Garbage":
start_time = time.time()
...
end_time = time.time()
if (emptygarbagetask = "Swipe Down") and (end_time - start_time <= 5):
...
...


Related Topics



Leave a reply



Submit