• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reading input with java.util.Scanner

 
Greenhorn
Posts: 4
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done sorting it out
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic