• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner and Int not compatible?  RSS feed

 
Shane Williams
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to make a little program where when the user is prompted, they can enter a password and receive one of two answers- Correct or Incorrect.
When I say my error message is "incompatible operand types Scanner and int".

Here is my code:


Does anyone have any suggestions?
P.S. I'm new to the language, just started today so simple terms please!

-Thanks
 
Mark King
Ranch Hand
Posts: 55
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner is a class for reading in inputs from a source, and returning the necessary information, Scanner has many methods for returning various types of information. Read the java.util.Scanner Javadoc for more information.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shane Williams wrote:


Welcome to the Ranch.
Like the saying goes, one cannot compare apples and oranges!

In the code above, you have declared userInput as a Scanner type and you are trying to compare it with 77 (which is an int primitive)
Think of Scanner as a tool. You use the tool to obtain data (user input in this case)

In pseudo code
1) Select a tool (done)
2) Obtain input (to be done)
3) Compare input with known value (done)

For step 2 above, like Mark correctly suggested, check out the API docs for methods available in the Scanner class which will let you take the user input in a comparable format (i.e. apple to apple, int to int)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!