title: Making a quiz with timer on java
body: <p>I am making a timed quiz where there should be a timer on each question.</p>
  
  <p>I want my code to output a question for 20 seconds and ask for an input for the answer and when the time is up, question number 2 should show up. I am stuck on what should I do to have that, a reference is enough, I just don't know what to read on about, I am using <code>java.util.Timer</code> and <code>TimerTask</code>.</p>
  
  <p>I also tried </p>
  
  <p><code>ExecutorService</code> but i only see examples that it shutsdown after the timer, i need to post another question after the timer ends, not shotdown the program since i need a time limit for each question and not a timer for the whole quiz.</p>
  
  <p>I tried this </p>
  
  <pre><code>if(seconds&lt;=20)
  {
      question1();
  }
  else if (seconds&lt;=40||seconds&gt;=21)
  {
      new ProcessBuilder("cmd","/c","cls").inheritIO().start().waitFor();
      question2();
  }
  </code></pre>
  
  <p>I also tried using while</p>
  
  <pre><code>while(seconds&lt;=20){
      question1();
  }
  while(seconds&lt;=40||seconds&gt;=21){
      new ProcessBuilder("cmd","/c","cls").inheritIO().start().waitFor();
      question2();
  }
  </code></pre>
  
  <p>This is what my question method looks like.</p>
  
  <pre><code>public static void question2(String... args) throws 
      IOException, InterruptedException
  {  
      System.out.println("");
      System.out.printf("%72s\n","QUIZ FOR PHILIPPINE HISTORY");
      System.out.println("");
      System.out.printf("%64s\n","Question #2");
      System.out.println("");
      System.out.println("\t\t\t\t\t     WHO DISCOVERED PHILIPPINES?");
      System.out.println("\n\t\t\t\t\t\tA. Fernando Magellan");
      System.out.println("\t\t\t\t\t\tB. Ferdinand Megallan");
      System.out.println("\t\t\t\t\t\tC. Ferdinand Magellan");
      System.out.println("\t\t\t\t\t\tD. Fernando Poe");
  
      System.out.println("\n\t\t\t\t\t\tTYPE YOU ANSWER HERE: ");
      char answer2 = a.nextLine().charAt(0);
      switch (sagot2)
      {
          case 'B':
          case 'b':
              score++;
              seconds = 21;
              System.out.print("You are CORRECT!");
              break;
          default:
              System.out.print("You are WRONG");
              seconds = 21;
              break;
      }
  }
  </code></pre>
  
  <p>This is the beginning of my code plus the timer and timertask.</p>
  
  <pre><code>import java.util.Timer;
  import java.util.TimerTask;
  import java.util.Scanner;
  import java.io.IOException;
  public class q2 {
  static int seconds = 0; 
  static Scanner a=new Scanner(System.in);
  static Timer timer = new Timer();
  static int number = 1;
  static int score = 0;
  public static void mema(){
      TimerTask task = new TimerTask () 
      {
          public void run() 
          {
              seconds++;
              System.out.print(seconds);
          }
      };
  
      timer.schedule(task,1000,1000);
  }   
  </code></pre>
  
  <p>I also tried using this but it doesnt do the other method after 5 seconds.</p>
  
  <pre><code>long start = System.currentTimeMillis();
  long wait = 5000;
  long end = start + wait;
  long end1 = end + wait;
  
  while (System.currentTimeMillis() &lt; end)
  {
  question1();
  }
  while (System.currentTimeMillis() &lt; end1 || System.currentTimeMillis() &gt; end)
  {
  question2();
  }
  </code></pre>