Results 1 to 11 of 11
- 03-28-2011, 09:25 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
Counting chars read each minute, infinite loop
Hi again,
I have a problem which I do not know how to solve.
Making a user-friendly program that counts how many letters(a-z or A-Z) the user types each minute, prints it on the screen, starts over in an infinite counting loop. As it is supposed to be user friendly I would want an easy way to quit the program?
Suggestions most appreciated :)
-
use getTimeInMillis() before the user starts typing, get another instance of getTimeInMillis() once the user stops typing, subtract the former from the latter, divide total character count (i.e. how many letters were typed during that period) by your previous answer; that should give you your final answer of characters typed per millisecond, so multiply by 60000 to get characters typed per minute.
- 03-28-2011, 11:47 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
The program is supposed to announce every 60 seconds how many chars were read from terminal in the past 60 seconds, and so it will continue in an infinite loop. Program will continue until it is stopped
- 03-28-2011, 11:58 PM #4
Maybe you need to use threads and a Timer.
- 03-29-2011, 12:10 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
I haven't learned about that yet, it is suggested to use for loop. But I'm nearly blank on how to put this up
- 03-29-2011, 01:11 AM #6
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
I suppose if you were advised to only use a for loop, you could use Thread.sleep() and count up time like that, but that wouldn't be accurate since computing takes time as well.
- 03-29-2011, 01:26 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Perhaps you could have a clause in the loop that is something like
Java Code:if(currentTime - startTime == (divisor of a minute)){ Print the amount of characters typed }
- 03-29-2011, 01:37 AM #8
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
But what is the easiest way to get it to start reading at a certain point? ie. ask user to start the process by pressing enter, and what would be the easiest way to specify which characters are legal here?
- 03-29-2011, 02:01 AM #9
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
1.
2. You would have to use if statements likeJava Code:Scanner in = new Scanner(System.in); String line; int charactersTyped; for(; ;){ //infinite loop until user enters start line=in.nextLine(); if(line.equals("start")){ for(; !line.equals("stop");){ // infinite loop until user enters stop charactersTyped += line.length()-1; // subtract by one because if a line has 7 letters it has 8 indexes, and line.length() returns the indexes line=in.nextLine(); } } }If you really want to be specific and only store the legal characters from a line then it would be more involved than thatJava Code:if(line.contains("b")) System.out.print("Invalid character.");Last edited by Solarsonic; 03-29-2011 at 02:38 AM.
- 03-29-2011, 02:53 AM #10
Member
- Join Date
- Mar 2011
- Posts
- 19
- Rep Power
- 0
This is what I've made so far, as you can see it lacks most things
Java Code:import java.util.Calendar; public class Main { public static void main(String[] args) throws Exception { int input = 0; long time = 0; int tall = System.in.available(); for (int i = 0; i < tall; i++) { do { input = System.in.read(); time = System.currentTimeMillis(); } while ((input > 64 && input < 91) || (input > 96 && input < 123)); System.out.println(i); } } }
-
Similar Threads
-
infinite loop
By javapink in forum New To JavaReplies: 19Last Post: 03-06-2011, 02:28 AM -
how to end infinite loop
By search4survival in forum New To JavaReplies: 14Last Post: 10-25-2010, 08:59 AM -
Infinite loop
By jDennis79 in forum New To JavaReplies: 7Last Post: 08-13-2010, 11:45 PM -
Infinite Loop
By bosoxfan in forum New To JavaReplies: 3Last Post: 02-22-2010, 01:34 AM -
Infinite Loop
By rclausing in forum New To JavaReplies: 2Last Post: 01-23-2010, 10:11 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks