• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with Scanner input  RSS feed

 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. Sorry to post two topics in one day asking for help. Hopefully one day i will have enough knowledge to be the one giving advice!

I've been having a lot of fun over the past few days learning to use If statements and While loops and also using the Scanner. I've been making big long programs (ok incredibly short programs for an expert but for me they seem long). Most of my programs that have used scanner have involved numbers and assigning the next input to a variable. Now i'm trying to do it with a String variable and i'm having some problems.

Here is the code



The problem is with line 16. It's the second time in the program that i want to assign what the user types in to a variable. But for some reason, when running the program Eclipse it's not pausing to let me type in my hobbies and when running my program in JCreator it just won't run at all. The first time in the program where i want to assign what the user types in to a variable (their name) it works.

Is there a fault with my code? Any help would be hugely appreciated. I think i'm going to make a program called theSeducer, where it trys to seduce the person running the program.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are just learning how to write code, I'd scrap Eclipse and JCreator altogether. Run your code from a command line and see what happens. That's the best way to a) learn java, b) see if it's your code or something with the IDE.
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:If you are just learning how to write code, I'd scrap Eclipse and JCreator altogether. Run your code from a command line and see what happens. That's the best way to a) learn java, b) see if it's your code or something with the IDE.


I have ran the code in command line, using notepad and i get an error message there too. Can you see what is wrong with my code?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
post the EXACT text of the error message. It actually tells you a lot about what the problem is, once you get familiar with them.

Also, this isn't the complete code you have. There are no closing braces to the methods/classes.
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i will do. I just realised when i copied and pasted i didn't copy the brackets by mistake. And as i also copied and pasted into notepad i will have to try that again. I'll be back in a sec.
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i've having problems with notepad. I need to keep trying with that. This is the exact code. And i don't get error messages . It just doesn't run how i would have expected. I would expect for it to let me input what my hobby is but it doesn't.


 
Ranch Hand
Posts: 56
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that problem, the nextInt() reads just the number from the input but not the enter you pressed, that enter is read by the nextLine().
I forgot the solution but I will look it up later on.
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i'm now doing it in notepad and it is doing the same thing.

This is how the program is running. Where i say "computer says" that is what the computer prints. Where i say "i input", that is what i type in at the promt.


computer says: What is your name?
I input: neil
computer says: Hello neil
How old are you?
I input: 29
computer says: Well that is still pretty young
Do you have any hobbies?
Wow what a coincidence! I LOVE

---------------------------------------------------------------------------------


Ok This is what i think SHOULD happen.

computer says: What is your name?
I input: neil
computer says:Hello neil how old are you?
I input: 29
computer says: Well that is still pretty young
Do you have any hobbies?
I input: Playing guitar.
computer says: Wow what a coincidence! I LOVE playing guitar.
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Misha van Tol wrote:I know that problem, the nextInt() reads just the number from the input but not the enter you pressed, that enter is read by the nextLine().
I forgot the solution but I will look it up later on.


Ok thanks!
 
Misha van Tol
Ranch Hand
Posts: 56
Android Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A quick solution is to read the scanner twice after the nextInt()

It works but is not really a nice solution.
 
Neil Cartmell
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Misha van Tol wrote:A quick solution is to read the scanner twice after the nextInt()

It works but is not really a nice solution.


Thanks that solved the problem.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
i use the next() method from Scanner class for taking input as String. It works perfectly fine. Try using it instead of nextLine()
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I discussed a similar problem here; you need to get used to the counter-intuitive way Scanner sometimes handles line-end characters. See whether that link is any help.
 
Anupama Sudhakaran
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just for my knowledge, wont using next() solve the problem you are talking about?
i have not used nextLine() till now and so dont know how it works
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!