Results 1 to 9 of 9
  1. #1
    Mayur is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default 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.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Post the code that gives the errors.
    P.S Do you know about the String.toCharArray() method?

  3. #3
    Mayur is offline Member
    Join Date
    Jan 2009
    Posts
    24
    Rep Power
    0

    Default

    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;
    	}
    And no I dont know about String.toCharArray()...googling it now.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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(""));

  5. #5
    suhassr is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    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)

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by suhassr View Post
    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)
    They didn't say they want to split sentences into words. Rather into characters. See the replies already posted.

  7. #7
    suhassr is offline Member
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Default

    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.

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    just pass like this

    ex : char data[] = {'a', 'b', 'c'};
    String str = new String(data);
    Ramya:cool:

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    just pass like this

    ex : char data[] = {'a', 'b', 'c'};
    String str = new String(data);
    You guys need to read the posts made by the OP.

Similar Threads

  1. Convert a vector to a string array
    By orchid in forum New To Java
    Replies: 4
    Last Post: 02-24-2010, 02:31 AM
  2. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 06:09 AM
  3. Convert Comparable object to string or char
    By ScKaSx in forum New To Java
    Replies: 4
    Last Post: 01-25-2009, 02:02 PM
  4. Replies: 9
    Last Post: 09-11-2008, 02:51 AM
  5. Cannot convert from char to String error
    By sondratheloser in forum New To Java
    Replies: 1
    Last Post: 12-13-2007, 09:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •