• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why isn't my simple code working?  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

So I'm just wondering if someone could explain why my simple code isn't working.

 
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried to compile the code? Error msg is your friend. I see the error but i won't tell you till you tried.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

As well as the compiler error, please go through the methods of a Scanner; there is a method which will give you the number directly so you don't have to use nextLine.
 
Jonas Deichmann Bengtsson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I have seen the error message. It goes "Exception in thread "main" java.lang.Error: Unresolved compilation problem: Incompatible operand types Scanner and int". Problem is for me where I am right now - this is sounding like greek.

I know that the line with the "if" statement most likely is at fault since eclipse is saying so. Problem could be that I can't use "myScanner" and an integer in the same condition?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are new to Java then i recommend not to use any IDE. Try to write programme on notepad++.

Check the line on which problem is coming. Compare variables whether they are same type or not.
 
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're comparing the scanner to the int 5, when you should be comparing the line string to the string "5" with equals().
 
Jonas Deichmann Bengtsson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the end I got it working like I wanted (with the help from another program though). Still feel like I don't really understand what I'm doing lol. Hopefully the Head First Java book will be a helpful for me to better understand why I'm doing things and why they're working; master the fundamentals essentially.

Thanks for your replies guys!

Final code:
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tyson Lindner wrote:You're comparing the scanner to the int 5, when you should be comparing the line string to the string "5" with equals().
Disagree. I think comparing the int with the == operator as JDB did, is correct.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Tyson Lindner wrote:You're comparing the scanner to the int 5, when you should be comparing the line string to the string "5" with equals().
Disagree. I think comparing the int with the == operator as JDB did, is correct.


I was referring to the original code, not the code above your post.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short version is that this:

is like saying "if my telephone is equal to "Hello".

That sentence doesn't even make sense. a telephone is a phone...it can never be equal to a word.

What you need to do is something like this:

if "the person on the other end of the telephone makes a noise like 'hello'"

you USE the telephone to GET some kind of information.

In your second code example. you are USING the scanner to get some information, and then comparing the information against a known quantity:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!