• Post Reply Bookmark Topic Watch Topic
  • New Topic

reading input with java.util.Scanner  RSS feed

 
Raymond Dillon
Greenhorn
Posts: 4
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, this is my first post and first time joining a forum. I'm a beginner in the Java world and was hoping that someone might be able to help me out. I've been working through a book called Learn Java the Hard Way which was all going well until I got to an exercise that involved getting input from the user. The program was compiled perfectly and ran initially. I have the code exactly as it is in the book (my eyes are almost bleeding from checking and double checking) so I can't work out where I've went wrong. When the program runs the first question that comes up is: "What city is the capital of France?" which then the user should be able to type their answer, hit enter then the next question comes up. The problem is I cannot input anything whatsoever or hit enter to move on. I have included the code:



Cheers and hopefully it could help someone else as well.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Thank you for using code tags on you very first post.
You didn't do it quite right, though. You need to enclose all your code within code tags.

Like this:
[code]
public static void main(String[] args) {
    System.out.println("Hello");
}
[/code]


This will produce:
You can press Edit button (this one ) to fix this.


As for your question, you are reading values using Scanner but you do nothing with those values.
Try assigning them to variables and/or printing.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome, Raymond.

Hey, maybe you could use code tags with your code and format it too: see how nice it looks?



Next, I want able to input something into this program if I was careful. The program throws an error with the wrong input. Are you running this from the command line? You really have to.
 
Raymond Dillon
Greenhorn
Posts: 4
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for both replies. Both helpful I appreciate it and next time there will be code tags!

Now here's the news, I've been running all the programs for the previous exercises on Powershell. This one has been the only one where I haven't been able to get it running. I fired up Command Prompt and tried. It is working absolutely fine. I'm sure I sound like a clampit. I'm trying to stick to exactly everything in the book where it recommended powershell, so I don't get ahead of myself. Is this the norm then or does Powershell not run certain programs? Strange. Cheers
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You know that you are getting input from the Scanner, but you appear not to be using it?
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The problem is I cannot input anything whatsoever or hit enter to move on.

This is serious. You cannot input anything nor hit the enter key? Your code as it is will compile fine and run fine if the inputs are in line with those required. Otherwise it will throw an exception.
 
Raymond Dillon
Greenhorn
Posts: 4
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

You know that you are getting input from the Scanner, but you appear not to be using it?


Thanks! Aye, it's all good now. I think I needed to look away from the screen for a minute then look back and see the problem! I needed to run it in Command Prompt. Working perfect now. On to the next Exercise Cheers
 
Raymond Dillon
Greenhorn
Posts: 4
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Ngom wrote:

The problem is I cannot input anything whatsoever or hit enter to move on.

This is serious. You cannot input anything nor hit the enter key? Your code as it is will compile fine and run fine if the inputs are in line with those required. Otherwise it will throw an exception.


Hi Paul, I appreciate your help. I was thinking the same thing, there would have been an exception had there been a problem with the code. It turned out I was running it in the wrong place. Thanks!
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It turned out I was running it in the wrong place. Thanks!

You are welcome. A good way of learning from examples is to try to tweak them to meet new requirements. For instance, you could save the inputs in variables, print output to screen and handle possible errors. I wish you good continuation!
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done sorting it out
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!