• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

While loop exit using sentinel value

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm having trouble trying to get this program to exit. Instead of exiting it is using the word exit as an input and displays if a vowel was found. Basically I need it to read exit so it will end the while loop not as just another input used for searching for vowels. Ignore the lowercase indentation I just saw that.
 
Rancher
Posts: 3441
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at line 16 and then line 17.  
16 forces lowercase
17 looks at an UPPERCASE String not at the contents of a variable

Also look at line 12

Note: Use the equals method to compare Strings, not the == or != operators
 
Nathan Montgomery
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Look at line 16 and then line 17.  
16 forces lowercase
17 looks at an UPPERCASE String not at the contents of a variable

Also look at line 12

Note: Use the equals method to compare Strings, not the == or != operators


So I updated my code.

Now I am getting an error "Exception in main thread"
 
Norm Radder
Rancher
Posts: 3441
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

am getting an error "Exception in main thread"


Please copy the full text of the error message and paste it here.  It should give the type of exception and the source line where it happened.
 
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're making the loop far too complicated. The idiom for using a sentinel value with a while loop is this:

Your code does this:

Notice how "do something with value" is nested two levels in, under the if-statement. Notice how the idiomatic form doesn't need an if-statement. You do repeat logic before the while and inside the while to get the value though but the ugliness can be mitigated by pulling the details out to private method, like so:
 
When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!