• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!");
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic