954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Split a line?

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][b][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?

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 

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

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 

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

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You