wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Taking a string, and putting the characters into an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Taking a string, and putting the characters into an array" Watch "Taking a string, and putting the characters into an array" New topic

Taking a string, and putting the characters into an array

Logan Knecht

Joined: Jun 05, 2008
Posts: 16
I'm trying to figure out how to put a string into an array so that each letter is put in a spot.

It would take a word like hello and have

test[0] = "h"
test[1] = "e"

and so forth.

The reason I'm trying to do that is because I want to take the numbers that someone could enter and base them off of the numbers column, so that the tens provide a response specific to a prefix, like twenty or thirty, and the ones column is specific to that.

Thanks in advance.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19656

String has a method called toCharArray that will return a char[] with each character of the String.

How To Ask Questions How To Answer Questions
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
How are you going to use the contents of the test array? Your example shows test to be defined as: String[] test;
The String class has several methods for getting at individual characters.
Scan thru the API doc to see if any of them is what you need.
Joseph Fisher

Joined: Jun 09, 2008
Posts: 5
Couldn't you also use the charAt() and length() methods also?
Beth Laguardia
Ranch Hand

Joined: Jun 01, 2008
Posts: 34
What you can do is put create a variable that will hold the valu of the string, then you can use the temp.length() assuming that temp refer ti the value of String.

Then after that you can use temp.atChar().
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299

Rob has already given the appropriate response here. I think beyond that Logan might be getting confused. Let's wait and see if Logan asks for anymore help.

toCharArray() is the best way to get all the characters of a String into an array.

GenRocket - Experts at Building Test Data
Consider Paul's rocket mass heater.
subject: Taking a string, and putting the characters into an array
Similar Threads
creating JTable using data from .txt file
checking for neighbours in array
Acces table
Printing a range after finding a dash (console input)