• Post Reply Bookmark Topic Watch Topic
  • New Topic

For loop not working as documented on Java a Beginner's Guide  RSS feed

 
Pablo Jose Alvarez
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I'm going through Java A Beginner's Guide and found the following example, which shows that the condition of a For loop doesn't necessarily have to use the control variable



According to the book, the loop should run until the user presses 'S' on their keyboard.

When I run this code, this is the output:

Press S to stop
_

The loop doesn't start executing. If it hit Enter, then I get

pass# 0
pass #1

and every time I hit Enter I get two more passes.

Am I missing anything or is the book just plain wrong in that such expression can be used as the condition for a For loop? Unfortunately I wasn't able to find an Errata for this book.

Thanks.

 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't judge your interpretation of the book's message, but perhaps you can gain some insight to the program's behavior by examining the API for System.in.read(). Note that input from the keyboard includes the character(s) typed and the Enter key or <return>.
 
Pablo Jose Alvarez
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Brannon wrote:I can't judge your interpretation of the book's message, but perhaps you can gain some insight to the program's behavior by examining the API for System.in.read(). Note that input from the keyboard includes the character(s) typed and the Enter key or <return>.


Thanks, I'm a bit familiar with how System.in.read() works. It basically takes input from the keyboard and stores it as a numeric value, that's why I'm casting to a char. However, I find it weird that this value is not stored in any variable, so it's seems a bit strange IMO that we can use something like this as the condition of a loop.

This is what the book states:

The condition controlling the loop can be any valid Boolean expression. It does not need to involve the loop control variable. In the next example, the loop continues to execute until the user types the letter S at the keyboard

Thoughts?
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To advance your understanding of the read() method, try typing a number of characters and see what happens. Something like, "abcdefghi<return>". How many passes were shown? How many characters were typed?
 
Pablo Jose Alvarez
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RESOLVED

I think I got it now:

The loop doesn't do anything but I' can't get out of the Loop until I submit 'S'. If I submit any other character, it will just print the "pass#" string but it won't exit the loop. I guess my interpretation of what the code was supposed to do was indeed wrong

Thanks for the help!
 
Greg Brannon
Bartender
Posts: 563
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your understanding wasn't wrong, just incomplete. These simple exercises are meant to not only demonstrate simple concepts but to be played with to explore and better understand the subtleties that everyone sees differently. It's great that you're typing out and running the examples, but don't be satisfied to just get them 'working.' Ask yourself, "What happens if i do this?" then make those minor modifications or vary the inputs to see what happens.

Keep coding!
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect that exercise is also there to show you just how awkward read() is to use and what a bad method it is.
 
Pablo Jose Alvarez
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I suspect that exercise is also there to show you just how awkward read() is to use and what a bad method it is.


Yeah, I don't like it at all but I believe they are using it because they are using char variables. But I've seen "scanner" being a lot more useful and easy to understand.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just about everything is easier to use than read(). Scanner is much easier, but it doesn't have a nextChar method. The nearest to that is:-
myScanner.next().charAt()
The read method doesn't return a char but an int, so you would have to cast it:-
char c = (char)System.in.read();
Since Scanner uses whitespace as a delimiter as default, any spaces are not counted as part of the “next” token, so next.charAt wouldn't return a space character unless you change the delimiter. There is an example with “fish” as delimiter in the Scanner documentation.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote: . . . There is an example with “fish” as delimiter . . .
Actually, it is space‑fish‑space, but you can change it to “fish” easily enough.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I suspect that exercise is also there to show you just how awkward read() is to use and what a bad method it is.


I'm curious.
Why is it a bad method?
It's low level and I'm not sure what it would be replaced by...
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it is so low‑level. It is slow. A readLine method is much faster. It doesn't read in a form you can used directly (char or String), but int which needs casting. Because it declares a checked Exception which the methods of Scanner doesn't do.
Why do you want such a low‑level method at all?

Now you know why I dislike it so.
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Because it is so low‑level. It is slow. A readLine method is much faster. It doesn't read in a form you can used directly (char or String), but int which needs casting. Because it declares a checked Exception which the methods of Scanner doesn't do.
Why do you want such a low‑level method at all?

Now you know why I dislike it so.


I've always assumed it was a leftover from converting the C libs.
I will admit to never having used it, as the read(byte[]) methods do everything needed...but still, doesn't make it a bad method. Well, apart from the annoyance of an int conversion because they felt they needed a -1 for EOS.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote: . . .
I've always assumed it was a leftover from converting the C libs.
. . .
Probably is.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!