Forums Register Login

Scanner and Int not compatible?

+Pie Number of slices to send: Send
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
2
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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)
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 994 times.
Similar Threads
Trying to create OO based bank app
Problem in looping
Map<Integer, Integer> intMap = new Hashtable<Integer, Integer>(); does not work
if, else if, else trouble
Stumped
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:48:55.