• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unexpected type?  RSS feed

 
Collin Sampson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do I keep receiving a complier message reading: unexpected types?? The error is occuring at the method setCharacter() (lines 37 and 41 specfically), it doesn't make sense to me. I am using a string method with a String variable (character). To determine if the program will convert to degreesC or degreesF. Where did I go wrong?

 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use "if (character.charAt(0) == 'f')" instead. Note the single quotes to indicate a char. Double quotes are used for String objects.
Also pay attention to "==" vs "=".
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Koen Aerts wrote:Use "if (character.charAt(0) == 'f')" instead. Note the single quotes to indicate a char. Double quotes are used for String objects.
Also pay attention to "==" vs "=".


The == vs. = is the real culprit at the moment.

I you look closely at the error message, you'll see something like


I realize that's not totally intuitive, but what it's telling you is that when you do something = somethingElse, the something on the LHS has to be a variable, because you're assigning a value from the RHS. So if you have a varaible x, you can do x = 7, but you can't do 9 = 7, which is what you're trying to do there.

Once you fix that, you'll get something like:


So, again, the error message is telling you exactly what's wrong. And here it's quite direct and explicit. "Incomparable types" means exactly what it sounds like--you're trying to compare two types that can't be compared, like apples and oranges. It then goes on to tell you exactly which two types you're erroneously trying to compare: char and String.

So, it's telling you that character.charAt(0) == "f" is trying to compare a char and a String, and you can't do that. At that point, either you see the "f" and go, Oh, silly me, it should be 'f' of course, or, if you're not aware of the double-quote/single-quote String/char difference, you can still apply a bit of logic. You can conclude that since charAt() returns a char, and since the compiler isn't lying, "f" must be a String. So we can't compare the String "f" to the char that comes back from charAt().
 
Collin Sampson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I got that. Now I am trying to test the program but the tester I have created is not compiling. Here is the tester:



The error says that it cannot find either getDegreesC or getDegreesF.
Some help please.
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Koen Aerts wrote:


@Collin: Do you understand the difference?
 
Collin Sampson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I do, I forgot the () on both so the compiler was not looking for the methods. Right? Thanks for the help
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!