aspose file tools*
The moose likes Beginning Java and the fly likes i need help letting a user input anything all upercase, lowercase, or both. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "i need help letting a user input anything all upercase, lowercase, or both." Watch "i need help letting a user input anything all upercase, lowercase, or both." New topic
Author

i need help letting a user input anything all upercase, lowercase, or both.

alaina peeler
Greenhorn

Joined: Dec 04, 2005
Posts: 29
It's a simple game of rock, paper, and scissors. But i need to let the user input their play in all uppercase, all lowercase, or a combination of both. Im trying the toLowerCase() method but it's not working right. It will convert it but then it wont print out my statements. Here is my code
public static void main (String[] args)
{
String rock,paper,scissors;
int compvar;
String computerplay="";
String userplay;


Scanner scan = new Scanner(System.in);
System.out.println("Enter your play(rock, paper, or scissors) below: ");
userplay = scan.nextLine();
System.out.println(userplay.toLowerCase()); /**this is where i put it**//

Random generator = new Random();
compvar = generator.nextInt(3);

switch(compvar)
{
case 0:
computerplay="rock";
System.out.println("Computer play is rock. ");

break;

case 1:
computerplay="paper";
System.out.println("Computer play is paper. ");

break;


case 2:
computerplay="scissors";
System.out.println("Computer play is scissors. ");

break;
}





if(userplay.equals("rock") && computerplay.equals("paper") )

System.out.println("Paper covers rock. The computer wins! ");
else
if(userplay.equals("rock") && computerplay.equals("scissor"))
System.out.println("Rock crushes scissors. You win!");
else
if(userplay.equals("rock") && computerplay.equals("rock"))
System.out.println("You tie!");
if(userplay.equals("paper")&& computerplay.equals("rock"))
System.out.println("Paper covers rock. You win!");
else
if(userplay.equals("paper") && computerplay.equals("scissors"))
System.out.println("Scissors cuts paper. The computer wins!");
else
if(userplay.equals("paper") && computerplay.equals("rock"))
System.out.println("Rock covers paper. The computer wins!");
else
if(userplay.equals("paper") && computerplay.equals("paper"))
System.out.println("You tie!");
if(userplay.equals("scissors") && computerplay.equals("rock"))
System.out.println("Rock crushes scissors. The computer wins!");
else
if(userplay.equals("scissors") && computerplay.equals("paper"))
System.out.println("Scissors cuts paper. You win!");
else
if(userplay.equals("scissors") && computerplay.equals("scissors"))
System.out.println("You tie!");
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Strings are immutable. The toLowerCase method will create a new String without modifying the original String at all. If you want to use that new String in more than one place, you need to assign it to some variable. For example...

userplay = userplay.toLowerCase();


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5019
    
    8

Another example:

String u = "HELLO!";
String l = u.toLowerCase();

System.out.println(u); // HELLO!
System.out.println(l); // hello!


Junilu - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5019
    
    8

Alaina,

Have you learned much about arrays yet? If you have, you can significantly reduce the number of if-else statements you write by using an array as a lookup table. Here's an example:



The whereIs() method could be made significantly easier to read by making a few convenience methods:



Apply the same principle to find what wins over what and how, you'll be set.
[ December 12, 2005: Message edited by: Junilu Lacar ]
 
Consider Paul's rocket mass heater.
 
subject: i need help letting a user input anything all upercase, lowercase, or both.