-
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.
-
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.
-
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.
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks