Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Don't understand why it loops through multiple times...

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Don't understand why it loops through multiple times...

    Hello People,

    So I'm new to Java programming and I'm reading through Herbert Schildt's "A beginner's Guide" 5th ED. I have come across something that I don't fully understand. This the code that I'm refering too:

    class Example {

    public static void main(String[] args)
    throws java.io.IOException{

    int i;

    System.out.println("Press S to stop");

    for(i = 0; (char) System.in.read() != 'S'; i++){
    System.out.println("Pass #" + i);
    }
    }
    }

    So, I press 'S' and the for loop will stop counting. Well every time I press something other than 'S' the for loop runs multiple times. For example if I press 'R', this is the output running this program through the Command Line:

    R
    Pass #1
    Pass #2
    Pass #3

    Then I am able to input another value. I was confused why it ran through the loop 3 times before I was able to input another value. So I ran this same program through Netbeans and it would do the same thing only it would pass through the for loop 2 times.

    I'm just confused why it does this and was wondering if anyone had any insight into why?

    Thanks for any assistance, I appreciate it greatly!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Don't understand why it loops through multiple times...

    When you press a key, the read() method reads it. Enter is a key. It's being pressed will return characters.

    To see what character(s) are read when Enter is pressed print out the values returned by read() by executing the following and only pressing Enter:
          for(int i=0; i < 5; i++) {
             int x = System.in.read();
             System.out.println(Integer.toHexString(x));
          }
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Don't understand why it loops through multiple times...

    Quote Originally Posted by Norm View Post
    When you press a key, the read() method reads it. Enter is a key. It's being pressed will return characters.

    To see what character(s) are read when Enter is pressed print out the values returned by read() by executing the following and only pressing Enter:
          for(int i=0; i < 5; i++) {
             int x = System.in.read();
             System.out.println(Integer.toHexString(x));
          }
    thanks Norm for the response,

    I tried you code and the ENTER key returns back: 'd' and 'a'. I'm still confused as to why it does this. Is this standard for the ENTER key?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Don't understand why it loops through multiple times...

    Yes, I think it is. Enter is just another key and must send some byte(s) to the program when it is pressed.

    d and a are hex values. Did you also try the program with other keys to see what values they sent.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Discoveringmypath (January 26th, 2013)

  6. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Don't understand why it loops through multiple times...

    I did try other keys and got some other values as well; I would get the 'd' and 'a' regardless though. I read a little more in the book that I was reading and It shows how to use this loop after the original use of the read() method to prevent other key inputs:

    do{

    ignore = (char) System.in.read();

    }while(ignore != '\n');

    "ignore" is just another char variable. From what I can tell, When I press a key and then enter, it registers 3 different values (the key I pressed, and then the 'd' and 'a'). So this loop runs through the extra values that are registered and once the last one has gone through, I assume the read() method has a '\n' (newline). so the loop exits and continues on with the program.

    Am I understanding this correctly?

    The book that I'm reading says that when the ENTER key is pressed, it causes a carriage return (defined as 13 Hex: 'd') and line feed (defined as 10 Hex: 'a'). So this explains why the 'd' and 'a'.

    I can see how this loop can help when using the read() method. With java I started learning online (youtube and other sites), I hate the ever famous line, "Don't worry about how this works for now, just do it..." I like to understand things as I go. I think I finally understand whats going on. Thanks again for the help Norm, well appreciated!

Similar Threads

  1. [SOLVED] Gotten easy code by proffessor that I don't understand, do you?
    By matitorn in forum Java Theory & Questions
    Replies: 7
    Last Post: October 9th, 2012, 01:30 PM
  2. Don't understand void methods, need help!
    By alex067 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 9th, 2012, 07:02 AM
  3. Using the same scanner multiple times in code help
    By theostorm in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 16th, 2012, 11:30 PM
  4. Simply don't understand minimax...
    By Herah in forum Algorithms & Recursion
    Replies: 3
    Last Post: October 13th, 2011, 12:45 PM
  5. I don't understand what I'm supposed to do
    By dmcettrick in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 11th, 2011, 09:34 AM