Comparing Strings


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Comparing Strings

  1. #1
    Zuke Guest

    Comparing Strings


    I am trying to make a program that asks a yes/no question.

    So far...

    String playagain;
    String yes = new String ("yes");

    System.out.println("--- play the game ---");

    // Issues a prompt
    System.out.print("Would you like to play again (yes/no)?: ");

    // User types in response
    playagain = Keyboard.readString();
    playagain = playagain.toLowerCase();

    Now, I want a loop to "play the game" again if the user
    types in yes. In Java, how would I write "If the string you input
    called playagain matches the string called yes, run the game again
    and ask if I want to play again."
    I've tried things like if statements, but the compiler always tells me
    it cannot convert the string to a boolean value.
    I think a command involving "equals" is needed, but I'm not sure what to
    do
    and keep running into recurring problems with booleans.
    Thanks for any help.

  2. #2
    Ellen Khlystova Guest

    Re: Comparing Strings

    Try this:

    // Eliminate spaces around the user's answer - just in case.
    playagain = playagain.trim()

    // Compare user's response to "yes".
    if ( playagain.startsWith ( yes ) )
    {
    / /then
    }
    else
    {
    }

    Ellen.



  3. #3
    Kent Guest

    Re: Comparing Strings


    Try this:

    /////////////////////////////////////////////////////////////
    String playAgain = "yes";

    while ("yes".equals(playAgain));
    {
    System.out.println("--- play the game ---");

    //game code goes here

    System.out.print("Would you like to play again (yes/no)?: ");
    // User types in response

    playAgain = Keyboard.readString();
    playAgain = (playAgain == null ? "" : playAgain.toLowerCase());
    }
    /////////////////////////////////////////////////////////////

    This will loop continuously until the user types in something other than
    'yes' when they are prompted.

    The equals(String str) method returns true when the string on it's left equals
    the string passed into the method. The reason I wrote
    "yes".equals(playAgain)
    rather than
    playAgain.equals("yes")
    is because you are guaranteed that "yes" will never be null (and therefore
    you will never get a null pointer exception) but you are not so safe in using
    the second example because playAgain, although probably not in this example,
    has the potential to be null.

    This should also explain why I changed the last line of code to check whether
    playAgain is null before attempting to call toLowerCase on it.

    Kent


    "Zuke" <DanMan1138@aol.com> wrote:
    >
    >I am trying to make a program that asks a yes/no question.
    >
    >So far...
    >
    > String playagain;
    > String yes = new String ("yes");
    >
    > System.out.println("--- play the game ---");
    >
    > // Issues a prompt
    > System.out.print("Would you like to play again (yes/no)?: ");
    >
    > // User types in response
    > playagain = Keyboard.readString();
    > playagain = playagain.toLowerCase();
    >
    >Now, I want a loop to "play the game" again if the user
    >types in yes. In Java, how would I write "If the string you input
    >called playagain matches the string called yes, run the game again
    >and ask if I want to play again."
    >I've tried things like if statements, but the compiler always tells me
    >it cannot convert the string to a boolean value.
    >I think a command involving "equals" is needed, but I'm not sure what to
    >do
    >and keep running into recurring problems with booleans.
    >Thanks for any help.



  4. #4
    Thomas Skariah Guest

    Re: Comparing Strings


    To compare to strings you can use the methods equals() or equalsIgnoreCase().
    Both these functions return true if the two strings are the same. The first
    method is case sensitive.

    In your example, it should be.

    if (playagain.equals("yes")) { }

    or

    if (playagain.equalsIgnoreCase("yes")) { }



    "Zuke" <DanMan1138@aol.com> wrote:
    >
    >I am trying to make a program that asks a yes/no question.
    >
    >So far...
    >
    > String playagain;
    > String yes = new String ("yes");
    >
    > System.out.println("--- play the game ---");
    >
    > // Issues a prompt
    > System.out.print("Would you like to play again (yes/no)?: ");
    >
    > // User types in response
    > playagain = Keyboard.readString();
    > playagain = playagain.toLowerCase();
    >
    >Now, I want a loop to "play the game" again if the user
    >types in yes. In Java, how would I write "If the string you input
    >called playagain matches the string called yes, run the game again
    >and ask if I want to play again."
    >I've tried things like if statements, but the compiler always tells me
    >it cannot convert the string to a boolean value.
    >I think a command involving "equals" is needed, but I'm not sure what to
    >do
    >and keep running into recurring problems with booleans.
    >Thanks for any help.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center