• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't get past the infinite while loop using hasNext()  RSS feed

 
Vala Zadeh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The program I wrote counts the # of upper and lowercase letters in a string user inputs. I know that hasNext() does not advance past the input, but I have input.next() to advance. Still there is an infinite loop.

 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reading from System.in will always have a next input, it's from the keyboard.

You need to make a flagged exit--like if the return String is an empty string, "".
 
Vala Zadeh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:Reading from System.in will always have a next input, it's from the keyboard.

You need to make a flagged exit--like if the return String is an empty string, "".


sorry, I am a beginner. Kindly tell me which part I need to tweak.
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at your temp variable from your input.next(), if you get an empty string "", then you should do a break... so

That should be the equivalent of hitting <enter> without typing anything else.

Vala Zadeh wrote:
Les Morgan wrote:Reading from System.in will always have a next input, it's from the keyboard.

You need to make a flagged exit--like if the return String is an empty string, "".


sorry, I am a beginner. Kindly tell me which part I need to tweak.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:Reading from System.in will always have a next input, it's from the keyboard.
Spot on: the standard input stream is implicitly always waiting for a next input.
You need to make a flagged exit--like if the return String is an empty string, "".
I am afraid that isn't correct. Unless you change the delimiter on the Scanner, you can't get next() to return an empty String. The only way you can get hasNext() to return false is by terminating the input. In the case of file input, that means you have passed the last line. OTOH the input from System.in will continue until you terminate it with ctrd‑D (on Macs or *nix) or ctrl‑Z (Windows®). That closes System.in and you cannot reopen it.
I think that means that while (myScanner.hasNext()) ... is not suitable for use on System.in. You can try...or similar.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Vala Zadeh.

I suggest you use char literals while counting. Much more reliable than − 65;
myArray[c − 'a']++;
You do realise that kind of arithmetic will only work reliably for English text? It will go horribly wrong with German characters like ü and ß. If you look up those two characters in this Unicode chart, you will see those two characters won't fit into a 26‑element array.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

Anyway, why have you got so much code in main method? It should start your program only. Try to create some methods out of it, preferably small ones and call them appropriately.

Around for loop you're missing curly braces. Try not to avoid them even if loop body contains only one statement in it.
In fact that loop is redundant. What JLS says?
JLS wrote:Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10).
For type int, the default value is zero.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!