0

I'm writing a simple java program to ask random math questions. I'm just using the console so nothing fancy.

How can I time how long it takes the user to answer a question from when it is first asked to when they key in an answer and hit return?

Davis Broda
  • 4,102
  • 5
  • 23
  • 37
Aro
  • 494
  • 5
  • 20

3 Answers3

3

Call System.currentTimeMillis() to get the time in milliseconds before and after each question.

David Conrad
  • 15,432
  • 2
  • 42
  • 54
  • http://stackoverflow.com/questions/13062345/measuring-time-differences-using-system-currenttimemillis – Aro Sep 10 '14 at 00:09
  • @Aro This question is about timing how long it takes a human to answer a question. If you're suggesting using `System.nanoTime()` for that, I think it would be serious overkill. – David Conrad Sep 10 '14 at 15:39
  • My code works with System.nanoTime() but when I use System.currentTimeMillis() I keep coming up with a difference of '0'. I'm not entirely sure why...and that's probably beyond the scope of this question. – Aro Sep 11 '14 at 06:06
0

Use a date to save the time when the question was displayed for the first time.

import java.util.Date;

Date startDate = new Date();

Then after the answer was submitted, subtract the date from the current time

long totalMilliseconds = (new Date()).getTime() - startDate.getTime();
Simon Farshid
  • 2,636
  • 1
  • 22
  • 31
0

Simply save the date when the question is shown

Date start = new Date();

Do the same when an answer is received and then calculate the difference and parse it to seconds or minutes:

long diff = start.getTime() - end.getTime();
long diffSeconds = diff / 1000 % 60;  
long diffMinutes = diff / (60 * 1000) % 60; 
Paco Lagunas
  • 320
  • 1
  • 13