jQuery in Action, 3rd edition
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading strings with Scanner class" Watch "Reading strings with Scanner class" New topic

Reading strings with Scanner class

Alfonso Powers

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

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
Alfonso Powers

Joined: Nov 11, 2005
Posts: 4

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
It's not a secret anymore!