• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

equalsIgnoreCase on multiple String literals?  RSS feed

 
Greenhorn
Posts: 13
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Good morning!

I am working on a project that involves creating a game of Rock, Paper, Scissors against a computer opponent using the main method as a "traffic cop", directing traffic to other methods.

I am working on my user selection method and it will be an int value returning method based on String input from the user.

I figured to validate user input and bar invalid text I could use a while loop as a gate to the next part of the method (at least, I've done this in other assignments and the Prof. has not indicated this is incorrect)

My issue is my equalsIgnoreCase statement is throwing an error in Eclipse as I am trying to compare the user input string to "rock", "paper", or "scissors", as so:



I tried separating the string literals with or's ("||") as well and it eliminated the method error, but moved it to the literals, stating the first two were invalid.

Figured I'd "phone a friend" on this one because google searches seemed to only teach me about the difference between equals, equalsIgnoreCase, and compareTo; and maybe I missed it, but nothing references multiple literals.

Complete code for the method below, thanks for the assist and, as always, any critique/advice! (Pardon the no commenting, I do this last (I know, I know; bad habit))



 
Marshal
Posts: 13447
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
equalsIgnoreCase takes a single String argument. When you separated the string with the || operator, you created a Boolean argument which does not match the required argument type. You need three invocations of equalsIgnoreCase, one for each string value you want to check and the appropriate Boolean operator between each invocation.
 
Sheriff
Posts: 5931
155
Chrome Eclipse IDE Java Postgres Database Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And think carefully about what you want your logical expression to be.  Some people are tempted to write:
but what does that mean?  Think carefully about what would pass and what would not.

Also, a better way of writing: is
 
Master Rancher
Posts: 3189
119
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you can do with just one comparison:
 
Junilu Lacar
Marshal
Posts: 13447
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you go with Piet's suggestion and use a List, there's also the indexOf() method you might want to take a look at.
 
Piet Souris
Master Rancher
Posts: 3189
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent idea, hadn't thought of that.
 
Bartender
Posts: 2232
94
Eclipse IDE Google Web Toolkit Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a separate discussion, why would you want your users to type "rock","paper" or "scissors" in the first place ?
Even if you were writing a Command line program, I'd prefer a short-code like:

Enter your choice (1=Rock, 2=paper, 3=scissors) : [Let user type here]

or, if you want to go the swing way:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!