• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Help with User Input  RSS feed

 
Ayush Agarwal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am new to java. I created this simple bank application.
It has 3 class files.
Trouble is with user input.
If I use scanner then compataor doesn't work it shows ""Index out of range -1""
If I use Buffered Reader it never goes inside Switch and keeps showing Options to Press 1,2,3,4,5.
Please Help





 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Please UseCodeTags (← click this) when posting. I added them for you this time. Doesn't it look better?

You closed your bis instance and then try to read from it again.
And I'm not quite sure what your Comparator is supposed to do. Can you explain?
 
Ayush Agarwal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even after I removed the close statement it doesn't work can you please try to run this..
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check here what read() in BufferedReader returns.
Add some print statements after you use this method to see whether it is what you intended.
 
Ayush Agarwal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Check here what read() in BufferedReader returns.
Add some print statements after you use this method to see whether it is what you intended.


Used the print method and it's not returning the number I expected. What should I do now??
 
Ayush Agarwal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ayush Agarwal wrote:
Paweł Baczyński wrote:Check here what read() in BufferedReader returns.
Add some print statements after you use this method to see whether it is what you intended.


Used the print method and it's not returning the number I expected. What should I do now??


Just figured out "I am getting ASCII values of the numbers I am inserting". Why is that???
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ayush Agarwal wrote:Just figured out "I am getting ASCII values of the numbers I am inserting". Why is that???

Because that is what read() returns.
Javadocs I told you to read say:
javadoc wrote:Returns:
The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached.


What I'm goint to tell you is not the only way to read input.
Use readLine() method instead of read(). This methods returns as a String exactly what an user typed.
Once you have this String you can for example convert it into number. This is called parsing.
Look at methods in Integer class to find a static method that will do the job.
 
Ayush Agarwal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. You saved the day !!!
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for suggestion, why have you written everything is main.. Ideally it should have 1-3 line max...
 
Ayush Agarwal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:Just for suggestion, why have you written everything is main.. Ideally it should have 1-3 line max...

Thanks for the suggestion. It is because I have a "C" background and I just started to learn Java so still struggling with classes.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Use readLine() method instead of read(). This methods returns as a String exactly what an user typed.
Once you have this String you can for example convert it into number.

Seconded.

@Ayush: And if you want some tips about making this more generic, have a look at the UserInput page.

HIH

Winston
 
Ayush Agarwal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow great tutorial,helped clear all my doubts about user input. Can you please suggest few more tutorials for Core Java or a source where I can find them. Thanks in advance.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ayush Agarwal wrote:Wow great tutorial,helped clear all my doubts about user input. Can you please suggest few more tutorials for Core Java or a source where I can find them. Thanks in advance.

Glad you liked it. My articles are linked in my signature, at the bottom of all my posts, and you may find some more of them useful; but they're an eclectic bunch, written as and when I see a need (usually as a result of many posts on the same subject from these forums), so don't expect any "shining path".

Otherwise, our FAQ page is often a good source of information.

HIH

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!