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

  • SEARCH
  • Home
  • 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 3403670
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:17:21+00:00 2026-05-18T05:17:21+00:00

I’m writing a testing system and i all i want to do is to

  • 0

I’m writing a testing system and i all i want to do is to count how many seconds had user spent on this question. i.e. i print question(standard System.out.println), then wait 5 seconds and if within these 5 seconds user answered(through standard input), i want to keep this value.

If user hasn’t provided an answer in 5 seconds, it must skip this question and continue.

The problem is I’m reading user answers via Scanner object, and something like in.nextInt() is uncontrollable, I suppose.

How can I solve this problem? Here is fragment of my code without that functionality, can you give me some hints what to add?

    public void start() {
    questions.prepareQuestions(numQuestions);
    Scanner in=new Scanner(System.in);
    boolean playerIsRight=false,botIsRight=false;
    int playerScore=0,botScore=0;
    for (int i = 0; i < numQuestions; i++) {
        questions.askQuestion(i);
        System.out.print("Your answer(number): ");
        playerIsRight=questions.checkAnswer(i,in.nextInt()-1); //in.nextInt() contains the answer
        botIsRight=botAnswersCorrectly(i + 1);
        if(playerIsRight){ playerScore++; System.out.println("Correct!");}
        else System.out.println("Incorrect!");
        if(botIsRight) botScore++;
        System.out.print("\n");
    }
    if(botScore>playerScore) System.out.println("Machine won! Hail to the almighty transistors!");
    else if(playerScore>botScore) System.out.println("Human won! Hail to the power of nature!");
    else System.out.println("Tie. No one ever wins. No one finally loses.");
}
  • 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-18T05:17:22+00:00Added an answer on May 18, 2026 at 5:17 am

    I would use two threads in this case. The main thread writes questions, waits for answers, and keeps score. A child thread reads standard input and sends the answers to the main thread, perhaps via a BlockingQueue.

    The main thread can wait for five seconds for an answer by using the poll() method on the blocking queue:

    …
    BlockingQueue<Integer> answers = new SynchronousQueue();
    Thread t = new ReaderThread(answers);
    t.start();
    for (int i = 0; i < numQuestions; ++i) {
      questions.askQuestion(i);
      System.out.print("Your answer (number): ");
      Integer answer = answers.poll(5, TimeUnit.SECONDS);
      playerIsRight = (answer != null) && questions.checkAnswer(i, answer - 1); 
      …
    }
    t.interrupt();
    

    If this call returns null, the main thread knows that the child thread didn’t receive any input during that time, and can update the score appropriately and print the next question.

    The ReaderThread would look something like this:

    class ReaderThread extends Thread {
    
      private final BlockingQueue<Integer> answers;
    
      ReaderThread(BlockingQueue<Integer> answers) { 
        this.answers = answers; 
      }
    
      @Override 
      public void run() {
        Scanner in = new Scanner(System.in);
        while (!Thread.interrupted()) 
          answers.add(in.nextInt());
      }
    
    }
    

    Used on System.in, the Scanner will block until the user presses Enter, so it might happen that the user has entered some text but not yet pressed Enter when the main thread times out and moves on to the next question. The user would have to delete their pending entry and enter a new answer for the new question. I don’t know of a clean way around this awkwardness, since there’s not a reliable way to interrupt the nextInt() call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.