Results 1 to 4 of 4
  1. #1
    Cero.Uno is offline Member
    Join Date
    Feb 2008
    Posts
    2
    Rep Power
    0

    Default Comparison of Strings

    I'm making a java console program, I'm taking input from the user however I want the program to only succeed if the selection given (string) is valid to the choices purposed from the console.

    Java Code:
                    printGameMenu();
    		do
    		{
    			userChoice = getInput("Please make a selection");
    		}
    		while("".equals(userChoice));
    Since I had problems using the != operator not working when comparing a string (nightmare), for the moment of learning I'm using the equals method, my question is what is the alternative method to equals?

    I need this while loop to continue iterating WHILE the input is not equal to "q" or "p"

    q for quitting the program and p for playing the game.

    I need the opposite method to equals.
    Last edited by Cero.Uno; 02-10-2008 at 07:17 PM.

  2. #2
    hey
    hey is offline Member
    Join Date
    Dec 2007
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by Cero.Uno View Post
    I need the opposite method to equals.


    Java Code:
    while("".equals(userChoice)==false);
    added: equals is the right thing to use, while comparing strings, when you do this ==,!= you compare addresses of the strings, not theactual strings (remember String is an object)
    Last edited by hey; 02-10-2008 at 09:03 PM.

  3. #3
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    10

    Default Boolean variables

    Hello Cero.Uno

    You can solve this problem simply by using boolean variables. The String.equals() method returns a boolean result of true if your Strings are equal in value.
    Java Code:
    boolean loop = true;
    do{
        userChoice = getInput("Please make a selection");
        boolean quit = userChoice.equals("q");
        boolean play = userChoice.equals("p");
        loop = (quit [COLOR="RoyalBlue"]||[/COLOR] play) [COLOR="SeaGreen"]==[/COLOR] false;
    while (loop [COLOR="SeaGreen"]==[/COLOR] true);
    This code uses the logic operators || and ==. Google logic operators in Java, to find out more.

    Good luck. ;)
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  4. #4
    Roughedge's Avatar
    Roughedge is offline Member
    Join Date
    Feb 2008
    Location
    Dundee
    Posts
    4
    Rep Power
    0

    Default

    Dead easy, just put the '!' before your condition. For example:

    Java Code:
    public class TestApp 
    {
        public static void main(String[] args)
        {
            int count = 0;
            System.out.println("Starting program");
            String testString1 = "hello";
            String testString2 = "world";
            
            while(!testString1.equals(testString2) && count != 2000)
            {
                count++;
                System.out.println("Number "+count+" This means it is not equal");
                
            }
        }
    
    }
    The count is there just to stop the infinite while loop that killed my computer!

Similar Threads

  1. Subtracting Strings
    By ravian in forum New To Java
    Replies: 7
    Last Post: 10-08-2009, 06:26 PM
  2. String comparison
    By abhiN in forum New To Java
    Replies: 2
    Last Post: 04-09-2008, 04:47 AM
  3. Date comparison
    By Rageagainst20 in forum New To Java
    Replies: 0
    Last Post: 12-19-2007, 07:34 PM
  4. Parsing Dates for Comparison
    By Rageagainst20 in forum New To Java
    Replies: 1
    Last Post: 12-19-2007, 06:50 AM
  5. String comparison
    By sireesha in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 01:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •