Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

simplest problem ever

Thread view: 
Christo - 19 Apr 2006 20:03 GMT
       while ((mFood != "Y") || (mFood != "N"))
       {
           mFood = User.inputString("ENTER Y for food or N for no food");
       }

well i think the code should say what i want to do

when i uinput Y i want the loop to end, same if i input N

but the loop isnt ending

help
Gordon Beaton - 19 Apr 2006 20:56 GMT
>         while ((mFood != "Y") || (mFood != "N"))
>         {
[quoted text clipped - 6 lines]
>
> but the loop isnt ending

There are two problems here:

- to compare Strings, use String.equals(), not ==.

- The loop condition says to continue as long as the input is unequal
 to either "Y" or "N". Since this is true of *every* String, the loop
 will never end. Change the condition to && (and) instead.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Sigmund Hansen - 21 Apr 2006 01:35 GMT
>>         while ((mFood != "Y") || (mFood != "N"))
>>         {
[quoted text clipped - 16 lines]
>
> /gordon

You might also want to use equalsIgnoreCase instead of just equals,
but that depends on whether or not your inputString method returns only
uppercase strings, or differentiates between input strings.
JC - 27 Apr 2006 10:43 GMT
>>>         while ((mFood != "Y") || (mFood != "N"))
>>>         {
[quoted text clipped - 21 lines]
> but that depends on whether or not your inputString method returns only
> uppercase strings, or differentiates between input strings.

You might also want to actually change the value of mFood each pass
through the loop...
Gordon Beaton - 27 Apr 2006 11:26 GMT
On Wed, 19 Apr 2006 20:03:07 +0100, Christo wrote:
>         while ((mFood != "Y") || (mFood != "N"))
>         {
>             mFood = User.inputString("ENTER Y for food or N for no
> food");
>         }

> You might also want to actually change the value of mFood each pass
> through the loop...

See that assignment statement?

Apparently User.inputString() takes a prompt, and returns the user's
input.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

JC - 27 Apr 2006 15:55 GMT
> On Wed, 19 Apr 2006 20:03:07 +0100, Christo wrote:
>>         while ((mFood != "Y") || (mFood != "N"))
[quoted text clipped - 12 lines]
>
> /gordon

<grin>
Silly me - didn't clock that.
</grin>


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.