Here is a simple Demo that would let you to know how to use java.util.Timer
.
import java.util.Timer;
import java.util.TimerTask;
import java.util.Scanner;
class Tester
{
static long i = 0;
public static void main(String[] args) throws Exception
{
Scanner scanner = new Scanner(System.in);
System.out.println("You have only 10 seconds to find the result");
System.out.println("What is the value of : 111111 X 111111 ");
Timer timer = new Timer("Timer");
timer.schedule(new TimerTask()
{
public void run()
{
if (i == 12345654321L)
{
System.out.println("Congrats!! you guessed the write answer :)");
}
else
{
System.out.println("Sorry Time is over. You couldn't guess the correct answer.");
}
System.exit(0);
}
},10 * 1000 , 1);
while (true)
{
i = scanner.nextLong();
if ( i == 12345654321L)
{
System.out.println("Congrats!! you guessed the write answer :)");
System.exit(0);
}
else
{
System.out.println("Try next guess :");
}
}
}
}
EDIT
Since I don't have your all code so I am posting here the solution for your answer on my basic assumption. Don't use Thread. Instead use java.util.Timer
. Your code would look as follows:
static String input=" ";//created a static variable input to take input
public static void main(String st[])
{
Timer timer = new Timer("Timer");
timer.schedule(new TimerTask()
{
public void run()
{
if (test.CheckGame(input))
{
System.out.println("Congrats!! you guessed the write answer :)");
}
else
{
System.out.println("Sorry Time is over. You couldn't guess the correct answer.");
}
System.exit(0);
}
},10 * 1000 , 1);//waits for 10 seconds
while (true)
{
System.out.print("Guess a word on the board! ");
input = scan.next();
if(test.CheckGame(input))
{
System.out.print("Good job! ");
System.exit(0);
}
else
{
System.out.println("Bad Guess. Try again ");
}
}
}