Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Incompatible operand types Scanner and int

 
Adam Hartman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a newby newb to Java and am trying to make a very simple program to help me understand the if statement better; however, whenever i use this code I keep getting this error: Incompatible operand types Scanner and int


Please use simple words when replying as I am newb.

[Edit - added code tags - see UseCodeTags for details]
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Adam. Welcome to the Ranch!

The compiler will have told you exactly which line the error is on. It's helpful to tell us what line that was - saves time. However, I can see what the problem is this time.

You've defined the variable x as holding a Scanner:
However, you then do this:
Here, you're trying to compare a Scanner object with a number, and they just aren't compatible. Which is exactly what the compiler is telling you: "Incompatible operand types Scanner and int".

What you need to do is read an int from the Scanner, and then compare that to 4.
 
Adam Hartman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

What you need to do is read an int from the Scanner, and then compare that to 4.
Not so sure how to do this :I
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, have a look at the documentation for Scanner, and you might find a method that will help.

One thing to point out: at the moment you're reading a line from the Scanner (with x.readLine()), and printing the result, but then you're throwing the value away. You need to read from the Scanner and assign something to a variable that you can use.
 
Adam Hartman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Hi Adam. Welcome to the Ranch!

The compiler will have told you exactly which line the error is on. It's helpful to tell us what line that was - saves time. However, I can see what the problem is this time.

You've defined the variable x as holding a Scanner:
However, you then do this:
Here, you're trying to compare a Scanner object with a number, and they just aren't compatible. Which is exactly what the compiler is telling you: "Incompatible operand types Scanner and int".

What you need to do is read an int from the Scanner, and then compare that to 4.


Wait... think I got it. Like this right?

 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the best way to be sure is to try it. But that looks right to me!

One thing you are doing there (and this is fine for trying things out) is that you're assuming that the user is going to input a number. Try it, and see what happens if they enter a word instead. You might want to think about how you could fix that. There's another method in Scanner that would be useful...
 
Adam Hartman
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I ran into another problem when messing around with the if statement The error is on line nine, and it's saying that four cannot be resolved into a variable


It's probably something obvious but... :/
 
Ryan Sykes
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couple of things:

1) It should be "four" not four. The former is a String, the latter just looks like an undefined variable name to the compiler.

2) More importantly, even if you fix the issue in 1), Strings in Java are Objects, not primary data types. As a result, you should not compare equality of Strings using ==, only use that for comparing primary data types like int, double, char, float, etc. When you do an == comparison on objects, you are actually checking if both object references are referring(pointing) to the same object. This however does not do a check based on the contents of the 2 objects, and is not a correct way to check equality. For Strings (and all other Objects), you will want to use the equals() method.

So it should be something like if(x.equals("four")). Note, you could also re-write it as if("four".equals(x)).

Also note that when you start writing your own classes, if you want to check the equality of 2 objects of your custom Class, you will need to implement your own equals() method for your class. Read any Java textbook/reference for more information on how to do that. Otherwise, Google is your best friend
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Hartman wrote:So I ran into another problem when messing around with the if statement The error is on line nine, and it's saying that four cannot be resolved into a variable


You've used four as if it was variable, without declaring it. Ryan's showed you what you should be doing if you want to check whether the user has entered "four". But I think you might have misunderstood what I meant. If I were you I'd stick with the original version - I'd expect users to enter the answer as a number. But with the code you had before, if the user entered "four", or "iv", or "Mary had a little lamb", it would throw an exception when it tried to convert that to a number. You could avoid that by checking first if the next thing the Scanner could read was a number (there's a method for that), and only doing the conversion if it was safe.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic