8 Replies Latest reply: Nov 20, 2006 8:40 AM by JosAH RSS

    Comparing chars...

    807599
      I'm trying to compare characters, in order to make selections for a program I need to write. I'm struggling to understand where I am going wrong though, so would appreciate any help.

      I have this so far:
      ....
      SimpleIO input=new SimpleIO(System.in);
      
      
              try{//read the choice and process it
                  
      
               String choice = input.readString();
               char selection = choice.charAt(0);
                
               while (selection != 'c' ||
                   selection != 'C' ||
                   selection != 's' ||
                   selection != 'S' ||
                   selection != 'e' ||
                   selection != 'E' )
               {
                 System.out.println("Invalid choice. Please enter again.");
                 choice = input.readString();
                 selection = choice.charAt(0);
               }
                  
               if (selection == 'C' || selection == 'c')
                 this.next_state=new GetFileName(); 
               else if (selection == 'S' || selection == 's' )
                 this.next_state=new AwaitSFT(); 
               else if (selection == 'E' || selection == 'e')
                 this.next_state=new Idle();
      ....
      The while loop never exits, and continously outputs my "invalid choice" message, even when given what I think to be a suitable entry (e.g. 'C').

      Is there something wrong with the way I have used the ||'s ?
      Thanks.
        • 1. Re: Comparing chars...
          791266
          <removed>

          Message was edited by:
          kajbj
          • 2. Re: Comparing chars...
            JosAH
            Let's simplify that condition in your while loop a bit: suppose the user is
            supposed to type 'a' or 'b', anything else is assumed to be wrong. You
            did this:
            while (input != 'a' || input != 'b') ... 
            Suppose I type 'b', it definitely isn't an 'a' so your condition will be true,
            which is not what you want. I normally do it like this:
            String responseChars= "...";
            while (responseChars.indexOf(input) < 0) ...
            kind regards,

            Jos
            • 3. Re: Comparing chars...
              807599
              edited

              Message was edited by:
              mez86
              • 4. Re: Comparing chars...
                791266
                Your loop says: "Invalid choice. Please enter again" in a loop because a character can't be c and C and s and S and e and E at the same time.
                • 5. Re: Comparing chars...
                  791266
                  @Op. You should use && and not ||
                  • 6. Re: Comparing chars...
                    807599
                    I did try using && instead of ||, but that didn't work either.

                    However, the other response in this topic does solve my problem. Thank you :)
                    • 7. Re: Comparing chars...
                      791266
                      I did try using && instead of ||, but that didn't
                      work either.

                      However, the other response in this topic does solve
                      my problem. Thank you :)
                      Yes that works, and might be easier to understand. But did you really try this?
                                  while (selection != 'c' &&
                                             selection != 'C' &&
                                             selection != 's' &&
                                             selection != 'S' &&
                                             selection != 'e' &&
                                             selection != 'E' )
                      • 8. Re: Comparing chars...
                        807599
                        Well, I thought I did.
                        I've just tried it using that again, because I can understand why it would work, and so was curious to see if it did or not. It worked using that method too this time lol, so there must have been another fault in my code somewhere last time I tried that which somehow caused it not to.

                        Thanks again for the help.