• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Scanner question  RSS feed

 
Glenn van Cauter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all!

Complete newbie inside the Java department, so bear with me please! Started "learning" and "practicing" Java a couple days ago and I'm having an issue with a scanner string.

What I want to achieve:

I want the console to give feedback based on the user input. For example. If the user types "1" inside the console, I want the console to reply with "YAY!", and if the input is other than "1", the console must reply with "Oh..".

I'm using the following code:



And the Console returns:



So, I typed in "1" but the console did not reply with "YAY!".

Could anyone please explain to me what I'm doing wrong?

Thanks!!

Regards,
Socuine

 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"1" != 1

The former is a String and the latter is an int.

Since you are reading in an int then you ought to check it against an int.

ETA: Also, you are trying to compare the reader to the String.
You probably want to compare what the user has actually entered (the int value).
 
Glenn van Cauter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:"1" != 1

The former is a String and the latter is an int.

Since you are reading in an int then you ought to check it against an int.

ETA: Also, you are trying to compare the reader to the String.
You probably want to compare what the user has actually entered (the int value).


Hi! Could you explain where I done wrong by pasting the code?
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glenn van Cauter wrote:
So, I typed in "1" but the console did not reply with "YAY!".

Could anyone please explain to me what I'm doing wrong?


You are checking if your Scanner object is equal to "1". And of course, a Scanner instance is never equal to a String instance -- as they are of different class types.

[EDIT: Oops. This is what happens when you walk away, mid post, to have a conversation. You wind up getting beaten to the response by minutes... ]

Henry
 
Liutauras Vilda
Marshal
Posts: 4665
320
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glenn van Cauter wrote:Could you explain where I done wrong by pasting the code?

 
Campbell Ritchie
Marshal
Posts: 55786
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

We try to avoid giving complete solutions because you will remember so much more if you work it out for yourself. What you want is to check whether the number entered is equal to 1, as you have already been told. If you have forgotten the details, remind yourself with a look at the Java™ Tutorials.
 
Glenn van Cauter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it working!



Campbell Ritchie wrote:Welcome to the Ranch

We try to avoid giving complete solutions because you will remember so much more if you work it out for yourself. What you want is to check whether the number entered is equal to 1, as you have already been told. If you have forgotten the details, remind yourself with a look at the Java™ Tutorials.


Thank you! It is indeed the best way to learn!

Thank you everyone else for your input.

Regards,
Glenn
 
Campbell Ritchie
Marshal
Posts: 55786
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done

Please look at lines 5‑6 because they sh‍ould really form one line.
 
Glenn van Cauter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Well done

Please look at lines 5‑6 because they sh‍ould really form one line.


You are right! Thank you for the feedback, I appreciate it alot!

Regards,
Glenn
 
Glenn van Cauter
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have another question about the usage of this script. I tried to make the code look nice by using another method to define the Scanner function.



But when I call the reader.nextInt() it tells me this:

reader cannot be resolved
 
Campbell Ritchie
Marshal
Posts: 55786
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are better ways to use a Scanner than having it in the current class, but find out about scope of variables. You are declaring it inside the constructor. Look in the JLS (=Java® Language Specification). Beware: the JLS can be difficult to read. I think you want the bit about local variable declarations.
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, variables are in scope (can be accessed) within the block they are declared in.  What's a "block"?  Code that is between braces "{}".  It's still good to read the JLS or other documentation on the basic idea.  Basically.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!