Results 1 to 7 of 7
- 07-29-2009, 10:51 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 32
- Rep Power
- 0
-
Look in the String API and you should find a method that does this for you.
- 07-29-2009, 11:25 PM #3
Member
- Join Date
- Jul 2009
- Posts
- 8
- Rep Power
- 0
You need to know the length of your String.
The size of your char[] is the same as your String's length.
Here's the code:
PHP Code:public class StringToArray { public static void main(String args[]){ String text = "someText"; int l = text.length(); char[] v = new char[l]; for (int i=0; i<l; i++){ v[i] = text.charAt(i); System.out.println(v[i]); } } }
-
I disagree with this and go with my original recommendation. If you look in the String API, you'll find the toCharArray() method which creates the array in one fell swoop. So if we are going to be spoon-feeding answers here (much better to learn to look into the API in my opinion), I think a simpler and more straight-forward solution would be:
Java Code:public class Fu1 { public static void main(String args[]) { String text = "someText"; // isn't this much simpler? char[] myCharArray = text.toCharArray(); } }
- 07-30-2009, 12:08 AM #5
Member
- Join Date
- Jul 2009
- Posts
- 32
- Rep Power
- 0
Thanks. With a little editing, the code worked perfectly!
EDIT: I was talking to webbusiness23, I never saw your post Fubarable as I didn't refresh since arriving at webbusiness' post.Last edited by ngc0202; 07-30-2009 at 12:10 AM.
- 07-30-2009, 01:04 AM #6
Some comments...
@ngc0202: Although the solution that webbusiness23 proposed is a good exercise, it would be better and more productive to use the String method as Fubarable suggested. Here's the link to the Javsa String APIs:
String (Java Platform SE 6)
@webbusiness23: Although you were were trying to help the OP, it's not a good idea to post complete working code... too much temptation to cut, paste & forget. It is a much better to give pseudo code, links, snippets, etc. This way the OP develops code and learns.
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 07-30-2009, 07:30 AM #7
Member
- Join Date
- Jul 2009
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
characters from a string into an integer
By 2potatocakes in forum New To JavaReplies: 7Last Post: 05-08-2012, 12:31 PM -
Shifting characters in array
By Mayur in forum New To JavaReplies: 2Last Post: 04-24-2009, 10:19 PM -
deleting characters from a String
By Hayzam in forum New To JavaReplies: 4Last Post: 08-29-2008, 12:14 PM -
how to get the characters one by one from a String?
By Somitesh Chakraborty in forum New To JavaReplies: 3Last Post: 08-20-2008, 08:56 PM -
Getting all characters in a String
By Alayna in forum New To JavaReplies: 2Last Post: 05-20-2007, 11:49 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks