This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Reading strings with Scanner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading strings with Scanner class" Watch "Reading strings with Scanner class" New topic
Author

Reading strings with Scanner class

Alfonso Powers
Greenhorn

Joined: Nov 11, 2005
Posts: 4
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.


I love learning.
Adam Price
Ranch Hand

Joined: Nov 11, 2005
Posts: 95
Do you mean as it is being typed (before the user hits enter?)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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()).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Alfonso Powers
Greenhorn

Joined: Nov 11, 2005
Posts: 4
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading strings with Scanner class