Results 1 to 11 of 11
  1. #1
    KAS
    KAS is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default 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 :)

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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.

  3. #3
    KAS
    KAS is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    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

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Maybe you need to use threads and a Timer.

  5. #5
    KAS
    KAS is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    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

  6. #6
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    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.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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
    }

  8. #8
    KAS
    KAS is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    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?

  9. #9
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by KAS View Post
    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?
    1.
    Java 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();
    }
    }
    }
    2. You would have to use if statements like
    Java Code:
    if(line.contains("b"))
    System.out.print("Invalid character.");
    If you really want to be specific and only store the legal characters from a line then it would be more involved than that
    Last edited by Solarsonic; 03-29-2011 at 02:38 AM.

  10. #10
    KAS
    KAS is offline Member
    Join Date
    Mar 2011
    Posts
    19
    Rep Power
    0

    Default

    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);
    
            }
        }
    }

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    if you make the method like i originally posted, you can simply ask it for the figure after every minute

    Java Code:
        try {
             while (true) {
                 //calculations;
                 Thread.sleep(60*1000);
             }
         } catch (InterruptedException e) {
             e.printStackTrace();
         }

Similar Threads

  1. infinite loop
    By javapink in forum New To Java
    Replies: 19
    Last Post: 03-06-2011, 02:28 AM
  2. how to end infinite loop
    By search4survival in forum New To Java
    Replies: 14
    Last Post: 10-25-2010, 08:59 AM
  3. Infinite loop
    By jDennis79 in forum New To Java
    Replies: 7
    Last Post: 08-13-2010, 11:45 PM
  4. Infinite Loop
    By bosoxfan in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 01:34 AM
  5. Infinite Loop
    By rclausing in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 10:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •