Re: simplest problem ever



Sigmund Hansen wrote:
Gordon Beaton wrote:
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");
}

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

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


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.

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



Relevant Pages

  • Re: simplest problem ever
    ... when i uinput Y i want the loop to end, ... The loop condition says to continue as long as the input is unequal ... but that depends on whether or not your inputString method returns only uppercase strings, ...
    (comp.lang.java)
  • Re: C# coding guidelines: use "this." or not when referring to member fields/properties within the
    ... Alphabetic for strings isn't quite so ... One example of where people go wrong is when they want to optimise loop ... implementation so that each iteration takes 10% less time will only ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Error handling in a Do Loop
    ... I made the changes you suggested to my strings - blanking them correctly now. ... have any further suggestion or pointers, ... > When loop is able to ping a computer and able to pull the information, ... when the loop does not ping a computer/unable to ...
    (microsoft.public.windows.server.scripting)
  • Re: Stupid Newbie Needs Help
    ... Without the loop the program works fine with the ... with 0-terminated strings, that way you can take advantage of C's ... hold up to 10 tokens, where each token may be up to 80 characters ... should give a clue of how the variable/constant/function/macro is ...
    (comp.lang.c)
  • Re: timeit module: am I missing something obvious?
    ... Why am I passing strings around when functions are ... best of 3 trials: 0.792 usec per loop ... quickly choosing number of iterations. ... - The result from the final pass through the convergence loop ...
    (comp.lang.python)