Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading strings with Scanner class

 
Alfonso Powers
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Java noob here. I'm wondering if there is any way to read String inputs from the user with the Scanner class? From my readings there does not seem to be a way and if that is the case what utility can I use to do this?

I basically just want to read each character from a users inputted string.

So if the user enters: Hello

I want to reference each letter one at a time. String[0] = H, etc., etc.

Thanks in advance.
 
Adam Price
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean as it is being typed (before the user hits enter?)
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sounds like you want to use the nextLine() method of Scanner, which returns a String.

Once you have the String, you can get the chars with String's charAt(x) method (or even toCharArray()).
 
Alfonso Powers
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Just wanted to thank you for your help. It worked perfectly. I used the length () and charAt() methods to do what I wanted to do, but the scanner nextLine() worked perfectly.

Thank you again.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic