permaculture playing cards*
The moose likes Beginning Java and the fly likes Problem with Incompatible Types. Help appreciated, information much more so. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with Incompatible Types. Help appreciated, information much more so." Watch "Problem with Incompatible Types. Help appreciated, information much more so." New topic
Author

Problem with Incompatible Types. Help appreciated, information much more so.

Michael Rippee
Greenhorn

Joined: Jun 30, 2011
Posts: 19
I have just started learning java, and am trying to write a program to employ some of the principles I'm learning. Unfortunatly, I'm having some problems with Incompatible types. Firstly, and most importantly, I don't really understand what "Incompatable Types" means. I know im somehow "mixing up" my int and scanner, but beyond that, I'm not sure what I'm doing wrong.

The specific error is on line 30. (I will make an arrow to show you all which specific line I mean)

To note, I am entirely new to both this forum and Java itself. Any, "Well why the hell didn't you just do this???!!!" comments are not appreciated. Contrarily, any, "These are the rules on this forum." comments are appreciated. I'm not entirely sure if this is where I was to post this question, and I'd be happy to move it if necessary.

Here is the code. Thank you many times for any help / info.



Not that anyone in their right mind would want to, but please don't use this code without asking me. Choppy though it may seem, it represents a good bit of time and effort to me.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18542
    
  40

Maveric Jones wrote:I have just started learning java, and am trying to write a program to employ some of the principles I'm learning. Unfortunatly, I'm having some problems with Incompatible types. Firstly, and most importantly, I don't really understand what "Incompatable Types" means. I know im somehow "mixing up" my int and scanner, but beyond that, I'm not sure what I'm doing wrong.

The specific error is on line 30. (I will make an arrow to show you all which specific line I mean)

To note, I am entirely new to both this forum and Java itself. Any, "Well why the hell didn't you just do this???!!!" comments are not appreciated. Contrarily, any, "These are the rules on this forum." comments are appreciated. I'm not entirely sure if this is where I was to post this question, and I'd be happy to move it if necessary.

Here is the code. Thank you many times for any help / info.



Not that anyone in their right mind would want to, but please don't use this code without asking me. Choppy though it may seem, it represents a good bit of time and effort to me.


Basically, you are correct -- a scanner object and an int are not compatible. In order for Java to compare whether two items are equal, they need to be the same type, or convertible. And there is no way to compare a scanner object to an int, or convert an scanner object to an int (or vica versa).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Michael Rippee
Greenhorn

Joined: Jun 30, 2011
Posts: 19
Awesome, thank you. What about 'double' objects? Are they compatible? int and scanner are two objects I use fairly often, and, as such, I get the above error code often. I am trying to figure out a way to repair or work around this in my code.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18542
    
  40

Maveric Jones wrote:Awesome, thank you. What about 'double' objects? Are they compatible? int and scanner are two objects I use fairly often, and, as such, I get the above error code often. I am trying to figure out a way to repair or work around this in my code.


First, int and double are not objects -- they are primitive types. And yes, int and double can be compared. If you try to compare an int with a double, the int will be converted to a double for the comparison.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18542
    
  40


BTW, did you really intend to compare an int with a Scanner object? or more likely, compare an int with the int that was returned from the scanner object previously?

Henry
Daniel Marti
Ranch Hand

Joined: Jun 08, 2011
Posts: 37
I believe you are misunderstanding what scanner is doing for you. Please watch this following snip of code:

Your scaner (NumberOfDiceScanner) is retrieving the next int written by the user and storing it in NumberOfDice. After it you are using the Scanner itself, instead of the int where you store the input.
should become
A scanner is just a tool to get the user input/file content/socket stream etc etc etc... It is not the object that holds the content.

Since you are starting, you are in the right place to be taught about good programming:
An attribute/parameter/variable should always start with a lower-case letter (NumberOfDiceScanner -> numberOfDiceScanner).
Michael Rippee
Greenhorn

Joined: Jun 30, 2011
Posts: 19
Wow. Thank you Daniel for that truly illuminating piece of information! (That sounded sarcastic but I promise it wasn't) I did not really understand the function of the scanner, and all I need to do is attribute it to some kind of variable (In this case, an int object) to get the whole thing to work seamlessly. The funny thing is that I knew I needed to attribute the scanner to an int, but failed to use that in in the later statement. Very sincerely appreciated.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38041
    
  22
... and welcome to the Ranch
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

On a higher level, "incompatible types" means that you are telling java to put a round peg in a square hole.

Java is a 'typed' language - everything is defined to be a specific type or class. Perl is an example of a language that is not. So in Perl, I can say

$daysOld = 3747;

I can now use the variable "$daysOld" as a string,, as an integer type, as a float, or whatever. Perl figures out from the context what I need to use it as.

Java doe NOT let you do that. When you create a variable, you tell it "This variable will always and forever be THIS type". So if you say

String myString = "123";

You CANNOT then try and at 1 to it to get 124. Java gets very upset. The operation/method expects a number, and you gave it a String, so it doesn't know what to do.

That's all that 'incompatible types' means.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Megha kharde
Greenhorn

Joined: Nov 24, 2010
Posts: 19
Why are you using two different Scanner objects?

Scanner DiceOrCoinScanner = new Scanner(System.in);
Scanner NumberOfDiceScanner = new Scanner(System.in);

however you could have used a single Scanner object. Am i right? If anybody knows please correct me.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38041
    
  22
You could probably use the same Scanner for both those purposes, yes. Unless there is something strange I haven't noticed.
Michael Rippee
Greenhorn

Joined: Jun 30, 2011
Posts: 19
I wondered about having to use two different scanners or not (By the way, is a scanner an object, primitive type or something else?) but I wasn't sure. Since I now know that scanners define variables, and don't need to / can't hold values themselves it doesn't seem like you would ever need more than one scanner. Unless, I suppose, they used different type of inputs? Does the System.in type use only the keyboard?
Daniel Marti
Ranch Hand

Joined: Jun 08, 2011
Posts: 37
To answer your first question, Scanner is an object. You can check the primitive types here, they are only a few and in no time you will know in a glance if something is of a primitive type or not.

What do you mean "scanners define variables"? You can be correct there, if you mean that Scanner can be a type of variable, but i am afraid you are falling into a huge mistake by meaning something else. Please elucidate better.

System.in represent the standard method of input (called standard input stream). Usually it will be the keyboard, but you can redirect System.in to something else, like a file. But as a start, consider System.in as the connection between Java and your keyboard.

About Scanners using different types of input... Scanners can be used to grab data from a lot of different sources. Sockets, Streams, input devices (keyboards, serial readers, usb connections, etc). Where you get the raw data is defined when you instantiate the Scanner.
Michael Rippee
Greenhorn

Joined: Jun 30, 2011
Posts: 19
Great info, thank you.

I was meaning about Scanners being used to define variables as in,
int myVarible = myScanner.nextInt()
Daniel Marti
Ranch Hand

Joined: Jun 08, 2011
Posts: 37
Michael Rippee wrote:
I was meaning about Scanners being used to define variables as in,
int myVarible = myScanner.nextInt()


You are using "define" wrongly. When you give a variable any kind of value, you are assigning a value to the variable.

equals to

Meaning, you are assigning the value retrieved with myScanner.nextInt() to myVariable. the scanner does nothing regarding the definition of the variable.
Michael Rippee
Greenhorn

Joined: Jun 30, 2011
Posts: 19
Awesome. What does it mean to "define" something then?
Daniel Marti
Ranch Hand

Joined: Jun 08, 2011
Posts: 37
A definition is... well in my opinion it is a word that should not be used on variables.
You can think of a definition when you declare and then assign a value to it if it is a variable or write its behavior when it is a method.
Example:

If you want more info on this, check this thread;

But you will see lots of documentation using define and declare as being the same thing. To be completely honest, you only see the difference between the both when talking about abstract methods.
Michael Rippee
Greenhorn

Joined: Jun 30, 2011
Posts: 19
Excellent thanks for the info.
 
Don't get me started about those stupid light bulbs.
 
subject: Problem with Incompatible Types. Help appreciated, information much more so.
 
Similar Threads
Needing help with a Do While loop. Getting an "illegal start of expression" error.
Incompatible operand types Scanner and int
coin flip assignment help
coin flip trouble!!
Wanting to ask user to keep "inputting" at the command prompt until a condition is reached