Results 1 to 9 of 9
- 10-11-2009, 11:00 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 24
- Rep Power
- 0
Convert Char Array to String Array
I am extremely stuck here. Pretty much I have a string from user input, that needs to be broken down into elements and put into a String array. I thought I could convert it into a char array (no problem) then use each element and set it equal to a respective element in a String array but I keep getting type mismatch errors. Any help would be greatly appreciated.
- 10-11-2009, 11:08 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Post the code that gives the errors.
P.S Do you know about the String.toCharArray() method?
- 10-11-2009, 11:10 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 24
- Rep Power
- 0
And no I dont know about String.toCharArray()...googling it now.Java Code:public static String[] stringToCharArray(String input) { int inputLength = input.length(); char[] temp = new char[inputLength]; String[] output = new String[inputLength]; for(int i=0; i<inputLength; i++) { temp[i] = input.charAt(i); output[i] = temp[i]; } return output; }
- 10-11-2009, 11:17 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
temp[i] contains a char not a String. So you can't do output[i] = temp[i];
PS. Have a look see what happens when you print
Java Code:Arrays.toString("MyInput".split(""));
- 10-12-2009, 10:54 AM #5
Member
- Join Date
- Oct 2009
- Posts
- 8
- Rep Power
- 0
What you can do is
String Temp[] = input.split(" ");
Temp now contains the elements of the input string in an array format split by a space
(or u can use whatever delimiter u want)
- 10-12-2009, 11:08 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 10-12-2009, 11:35 AM #7
Member
- Join Date
- Oct 2009
- Posts
- 8
- Rep Power
- 0
public static ->String[]<- stringToCharArray(String input
I think what Mayur meant was to break a given sentence into a String array which contains the individual words.
Thats why he is copying the word in temp to an output variable and returning that String [] variable.
so using the split function is the easiest.
- 10-12-2009, 11:36 AM #8
just pass like this
ex : char data[] = {'a', 'b', 'c'};
String str = new String(data);Ramya:cool:
- 10-12-2009, 11:41 AM #9
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
Convert a vector to a string array
By orchid in forum New To JavaReplies: 4Last Post: 02-24-2010, 02:31 AM -
String array to byte array?!
By Joe2003 in forum Advanced JavaReplies: 5Last Post: 02-28-2009, 06:09 AM -
Convert Comparable object to string or char
By ScKaSx in forum New To JavaReplies: 4Last Post: 01-25-2009, 02:02 PM -
Anyone know how to take info from a txt file and convert it to a char array?
By 2potatocakes in forum New To JavaReplies: 9Last Post: 09-11-2008, 02:51 AM -
Cannot convert from char to String error
By sondratheloser in forum New To JavaReplies: 1Last Post: 12-13-2007, 09:28 PM


LinkBack URL
About LinkBacks


Bookmarks