• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

String with while loop not working, please help me

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the code keep asking too input again, even i input a, s, c
 
Greenhorn
Posts: 10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To compare literals please use equals() method instead of ==. By using == you're actually comparing references rather than contents of variables. So modified version of your code will look like

You can find more information about the difference here:
https://www.javatpoint.com/difference-between-comparing-string-using-equals-equals-and-dot-equals-method-in-java

Edit: Welcome to the Ranch
 
Marshal
Posts: 79698
381
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please avoid ...while (true); that denotes an infinite loop, and, to me, an infinite loop is the epitome of a program gone wrong. If you want a do loop, write it like this:-No need for break; any more. Note how you can break the line so it doesn't go over the right margin of the paper or screen.
The link you were given isn't clearly written, but it gives the correct answer, as NM told you. You can rely on == always returning false in this program. One of our people, Winston Gutkowski, wrote an FAQ about it. I like to call == the, “same object operator,” even if that isn't its official name.
If you try something cimilar in C#, you will find that == gives different results.
Don't declare the String so early; it Java® it is best to declare local variables as late as possible:-  Improved version:-Probably better still, using var:-
 
Bartender
Posts: 10886
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell and I have different feelings about the use of infinite loops with break. Keyboard input methods are generally short so readability is not an issue and the loops are infinite until valid input has been entered.
 
Campbell Ritchie
Marshal
Posts: 79698
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Campbell and I have different feelings about the use of infinite loops with break. . . .

We do. Be grateful for small mercies; I could have changed the loop to a plain while loop with tests for the input in the loop heading. I shall leave that delight for another occasion.
 
Carey Brown
Bartender
Posts: 10886
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naadiyaar, this snippet of code you're working on is very familiar in text base user interfaces. So much so that very often a project will have a Keyboard.java file (or some other name) to collect all of those methods as static utility methods.  The methods should be written as generically as possible to allow for reuse. Taking your snippet as an example it might look like this.

 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic