simplest problem ever

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christo

    #1

    simplest problem ever

    while ((mFood != "Y") || (mFood != "N"))
    {
    mFood = User.inputStrin g("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

    #2
    Re: simplest problem ever

    On Wed, 19 Apr 2006 20:03:07 +0100, Christo wrote:[color=blue]
    > while ((mFood != "Y") || (mFood != "N"))
    > {
    > mFood = User.inputStrin g("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[/color]

    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

    --
    [ 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

    Comment

    • Sigmund Hansen

      #3
      Re: simplest problem ever

      Gordon Beaton wrote:[color=blue]
      > On Wed, 19 Apr 2006 20:03:07 +0100, Christo wrote:[color=green]
      >> while ((mFood != "Y") || (mFood != "N"))
      >> {
      >> mFood = User.inputStrin g("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[/color]
      >
      > 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
      >[/color]

      You might also want to use equalsIgnoreCas e instead of just equals,
      but that depends on whether or not your inputString method returns only
      uppercase strings, or differentiates between input strings.

      Comment

      • JC

        #4
        Re: simplest problem ever

        Sigmund Hansen wrote:[color=blue]
        > Gordon Beaton wrote:[color=green]
        >> On Wed, 19 Apr 2006 20:03:07 +0100, Christo wrote:[color=darkred]
        >>> while ((mFood != "Y") || (mFood != "N"))
        >>> {
        >>> mFood = User.inputStrin g("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[/color]
        >>
        >> 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
        >>[/color]
        >
        > You might also want to use equalsIgnoreCas e instead of just equals,
        > but that depends on whether or not your inputString method returns only
        > uppercase strings, or differentiates between input strings.[/color]

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

        Comment

        • Gordon Beaton

          #5
          Re: simplest problem ever

          On Wed, 19 Apr 2006 20:03:07 +0100, Christo wrote:[color=blue]
          > while ((mFood != "Y") || (mFood != "N"))
          > {
          > mFood = User.inputStrin g("ENTER Y for food or N for no
          > food");
          > }[/color]

          On Thu, 27 Apr 2006 10:43:57 +0100, JC wrote:[color=blue]
          > You might also want to actually change the value of mFood each pass
          > through the loop...[/color]

          See that assignment statement?

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

          /gordon

          --
          [ 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

          Comment

          • JC

            #6
            Re: simplest problem ever

            Gordon Beaton wrote:[color=blue]
            > On Wed, 19 Apr 2006 20:03:07 +0100, Christo wrote:[color=green]
            >> while ((mFood != "Y") || (mFood != "N"))
            >> {
            >> mFood = User.inputStrin g("ENTER Y for food or N for no
            >> food");
            >> }[/color]
            >
            > On Thu, 27 Apr 2006 10:43:57 +0100, JC wrote:[color=green]
            >> You might also want to actually change the value of mFood each pass
            >> through the loop...[/color]
            >
            > See that assignment statement?
            >
            > Apparently User.inputStrin g() takes a prompt, and returns the user's
            > input.
            >
            > /gordon
            >[/color]

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

            Comment

            Working...