Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0

    Exclamation (please help) convert a string to a string[]

    I want to convert a string (contains empty string " ") to a string[] with out " "

    Here is the source code:

    String[] s4 = " 1 10.25 3.3 0.32 ".split(" ");
    String[] s5 = null;
    int i1 =0;
    for (int i = 0; i<s4.length;i++){
    if (s4[i] != " "){
    s5[i1] = s4[i]; //here "java.lang.NullPointerException" error
    i1++;
    }
    }

    //expected result s5 = ["1","10.25","3.3","0.32"]

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,450
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gutou2008_2008 View Post
    I want to convert a string (contains empty string " ") to a string[] with out " "

    Here is the source code:

    String[] s4 = " 1 10.25 3.3 0.32 ".split(" ");
    String[] s5 = null;
    int i1 =0;
    for (int i = 0; i<s4.length;i++){
    if (s4[i] != " "){
    s5[i1] = s4[i]; //here "java.lang.NullPointerException" error
    i1++;
    }
    }

    //expected result s5 = ["1","10.25","3.3","0.32"]
    After you first line of code add this line:

    Java Code:
    System.out.println(Arrays.toString(s4));
    ... run it and see what happens; (you don't need all that other code).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Apr 2011
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by gutou2008_2008 View Post
    I want to convert a string (contains empty string " ") to a string[] with out " "

    Here is the source code:

    String[] s4 = " 1 10.25 3.3 0.32 ".split(" ");
    String[] s5 = null;
    int i1 =0;
    for (int i = 0; i<s4.length;i++){
    if (s4[i] != " "){
    s5[i1] = s4[i]; //here "java.lang.NullPointerException" error
    i1++;
    }
    }

    //expected result s5 = ["1","10.25","3.3","0.32"]
    The reason to why you are getting a nullpointer exception is because you never initialize "s5".
    If you want to split it to a String array that does not contain any whitespace, try doing this instead:
    Java Code:
    String[] s4 = " 1  10.25    3.3  0.32   ".trim().split("\\s+");
    Not sure if that is the best way of doing it, but it should work. s4 should contain the expected result.

    EDIT: Fixed the code a little
    Last edited by OutputStream; 04-19-2011 at 03:40 PM.

Similar Threads

  1. Convert String to Date?
    By bochra in forum New To Java
    Replies: 4
    Last Post: 11-15-2010, 10:41 AM
  2. convert obj to string
    By isme in forum New To Java
    Replies: 11
    Last Post: 06-14-2010, 10:54 AM
  3. How to convert a String into an Hexadecimal ?
    By ze snow in forum New To Java
    Replies: 7
    Last Post: 02-16-2010, 10:31 PM
  4. how to convert a string to an integer
    By LAW in forum New To Java
    Replies: 6
    Last Post: 11-09-2009, 03:29 AM
  5. convert a really big string (len 39) to bigdecimal
    By coolsig in forum Advanced Java
    Replies: 6
    Last Post: 06-14-2008, 02:48 AM

Posting Permissions

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