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!
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:
Code :
for(int i=0; i < 5; i++) {
int x = System.in.read();
System.out.println(Integer.toHexString(x));
}
Re: Don't understand why it loops through multiple times...
Quote:
Originally Posted by
Norm
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:
Code :
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?
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.
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!