File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Incompatible operand types Scanner and int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Incompatible operand types Scanner and int" Watch "Incompatible operand types Scanner and int" New topic
Author

Incompatible operand types Scanner and int

Adam Hartman
Greenhorn

Joined: Feb 25, 2012
Posts: 4
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

Joined: Apr 06, 2010
Posts: 4391
    
    8

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

Joined: Feb 25, 2012
Posts: 4

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

Joined: Apr 06, 2010
Posts: 4391
    
    8

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

Joined: Feb 25, 2012
Posts: 4
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

Joined: Apr 06, 2010
Posts: 4391
    
    8

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

Joined: Feb 25, 2012
Posts: 4
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

Joined: Jan 18, 2012
Posts: 58
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

Joined: Apr 06, 2010
Posts: 4391
    
    8

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Incompatible operand types Scanner and int