6 Replies Latest reply: Mar 13, 2007 7:01 AM by 796447 RSS

    Using a string with If-Else statement

    807599
      Hello All,
      I am attempting to write a program which asking for a two char code and then prints out what the code will execute. I tried to use a switch statement and had no success. I am now tring to use a If-Else statement and still can not get the program to recognize the two digit code that the user enters. Everytime it defaults to the final "else" condition. Below is my code:

      do
           {
           System.out.println("Please enter code:");
           code=keyboard.next();

           
                if (code == "SW")
                System.out.println("Share Withdrawal");
                else if (code == "CD")
                System.out.println("Check Disbursed");
                else if (code == "CR")
                System.out.println("Cash Received");
                else if (code == "SS")
                System.out.println("Share to Share xfer from one member to another.");
                else
                System.out.println("I'm sorry I do not recognize this code");
           }
           
      while (code != "quit");

      Please help me understand what I am missing. Thank you for any help!
        • 1. Re: Using a string with If-Else statement
          807599
          Never compare Strings with ==, use the equals() method.
          if(code.equals("SW"))
          • 2. Re: Using a string with If-Else statement
            807599
            In fact, a good "rule of thumb" is never compare objects with ==, use equals.
            When you are in a situation where you need to break this rule of thumb, it's
            usually obvious that you need to do so...
            • 3. Re: Using a string with If-Else statement
              807599
              I think "never" is too strong...
              I agree that in most cases it isn?t a good idea, but talking with another programmer he told me about the class WeakHashMap because if we don?t create a new String instance with String("asd") the key will be pointing directly to a constant in the String pool, hindering the weak reference pass to the garbage collection, and so, we don?t reach the targets of a WeakHashMap.
              • 4. Re: Using a string with If-Else statement
                807599
                Captain,
                The equals method worked perfectly to compare the string. Thank you for your help. One more question I have is how I would use a "not equal to" method. Of course in my code I tried the != operator in my while statement but just like your previous answer I assume that won't work with strings. Any suggestions? Thank you so much for your help.
                • 5. Re: Using a string with If-Else statement
                  masijade
                  I think "never" is too strong...
                  "rule of thumb" means a basic rule that fits most (if not all) cases. So never is completely acceptable when used after that "rule of thumb" phrase, as that phrase automaticaly moderates it.
                  • 6. Re: Using a string with If-Else statement
                    796447
                    Captain,
                    The equals method worked perfectly to compare the
                    string. Thank you for your help. One more question
                    I have is how I would use a "not equal to" method.
                    Of course in my code I tried the != operator in my
                    while statement but just like your previous answer I
                    assume that won't work with strings. Any
                    suggestions? Thank you so much for your help.
                    if (!myString.equals(anotherString)) ...
                    (note the exclamation point in the above -- the "not" operator)