Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Taking a string, and putting the characters into an array

 
Logan Knecht
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20551
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String has a method called toCharArray that will return a char[] with each character of the String.
 
Norm Radder
Ranch Hand
Pie
Posts: 1032
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Couldn't you also use the charAt() and length() methods also?
 
Beth Laguardia
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic