forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.java
37 lines (33 loc) · 1.08 KB
/
timer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.Timer;
import java.util.TimerTask;
import java.io.*;
public class timer {
private String str = "", str2 = "";
BufferedReader in;
Timer timer = new Timer();
boolean hasRun = false;
TimerTask task = new TimerTask() {
@Override
public void run() {
hasRun = true;
if (str.equals("")) {
System.out.println("you had enter nothing. Press 'Enter' to proceed to next ques.");
System.out.println("------------------------------------------");
}
timer.cancel();
}
};
public String getInput(int seconds) throws IOException {
timer.schedule(task, seconds * 1000);
System.out.println("Answer within " + seconds + " seconds: ");
System.out.print("Your ans: ");
in = new BufferedReader(new InputStreamReader(System.in));
str2 = in.readLine();
if (!hasRun) {
timer.cancel();
str = str2;
}
System.out.println("------------------------------------------");
return str;
}
}