Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6731485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:32:05+00:00 2026-05-26T10:32:05+00:00

I’m trying to implement a time limit a user has to answer a question

  • 0

I’m trying to implement a time limit a user has to answer a question in a quiz. Although I have found quite a bit on timmers I don’t know how to piece it all together.

I want the user to have 15 seconds to answer the question. If they answer it in time, it checks if answer the answer is correct and then asks them if they want to continue to the next question.

If the user gives no response in the 15 seconds then it should say that the answer is incorrect and gives them the option to move to the next question.

Here is what I have so far.

for(int i=0; i<quiz.getQuizQuestions().size(); i++){

        super.getQuestion(i);

        //While timer is less than 15 seconds

        getResponse(i, questionStart);

        //If time has run out output "You have run out of time"

        super.nextQuestion();


}

It is probably worth knowing:

super.getQuestion(i) is just printing the question being asked

getResponse() is waiting for keyboard input. if something is entered then it checks to see if the user is correct.

super.nextQuestion() asks the user is they want to move onto the next question

Thanks in advance

EDIT: It would also be amazing if it was easy to implement a counter that counted down from 15 when converting this into a GUI.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:32:06+00:00Added an answer on May 26, 2026 at 10:32 am

    uses ExecutorService and Future to make sure we read a line or interrupt it. Code is a little longer than I expected… Let me know if something is unclear:

    import java.util.concurrent.*;
    import java.io.*;    
    
    public class Test {
        public static void main(String[] args) throws java.io.IOException {
            Question q = new Question();
            System.out.println("You have 5 seconds: " + q.toString());
    
            String userAnswer = null;    
            ExecutorService ex = Executors.newSingleThreadExecutor();
            try {
              Future<String> result = ex.submit(new GetInputLineCallable());
              try {
                userAnswer = result.get(5, TimeUnit.SECONDS);
                if (Integer.valueOf(userAnswer) == q.getAnswer()){
                    System.out.println("good!");
                }
                else{
                    System.out.println("Incorrect!");
                }
    
              } catch (ExecutionException e) {
                e.getCause().printStackTrace();
              } catch (TimeoutException e){
                System.out.println("too late!");
                return;
              } catch (InterruptedException e){
                System.out.println("interrupted?");
                e.getCause().printStackTrace();
              }
    
            } finally {
              ex.shutdownNow();
            }
        }
    }
    
    
    
    class GetInputLineCallable implements Callable<String> {
      public String call() throws IOException {
        BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
        String input = "";
        while ("".equals(input)) {
          try {
            while (!inp.ready()) {
              Thread.sleep(100);
            }
            input = inp.readLine();
          } catch (InterruptedException e) {
            return null;
          }
        } 
        return input;
      }
    }
    
    
    
    
    class Question{
      int p1, p2;
      public Question(){
        p1 = 2;
        p2 = 3;
      }
      public String toString(){
        return String.format("%d + %d = ?", p1, p2);
      }
      public int getAnswer(){
        return p1+p2;
      }  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.