Okay im reading in lines of text e.g "abcdefg" and I need to split the line into an array of characters where each character is stored in its own element so its like [a][c][d][e][f][g]

I know how to do it with splitting words from poems etc... using something like txtLine.split(" ") but i dont know how to do it with thse characters. They have no spaces between them?

Recommended Answers

All 4 Replies

read it as a String and use the toCharArray() method

commented: Yes, indeed +13

This was also mentioned by myself and s.o.s. two days ago in your other post about the char array... :P

yeah i just managed to get it working. Thanks all!

One of the important things you should know as a Java developer is to find the classes in the standard library which contain the functionality you desire. As an example, since you need to operate on Strings and need to generate a char array, looking into the documentation of String and Character classes should be your first step. If that fails, try looking into the java.util package for utility methods which operate on your data. If you need help with text processing and formatting, java.text package should be good to go. This is a pretty good exercise and will actually make you aware of the functionality provided by the Java standard library.

If that fails, use your favorite search engine to give you a solution to your problem. In your case, searching for 'convert string to char array' hit the spot right on.

When everything fails, use discussion groups as your last resort. It's not too much work, really, and if you feel it is, then think that it for your own good. :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.