File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
alaina peeler
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another example:

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

System.out.println(u); // HELLO!
System.out.println(l); // hello!
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic