Results 1 to 3 of 3
- 04-19-2011, 12:26 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 1
- Rep Power
- 0
(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"]
- 04-19-2011, 12:32 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-19-2011, 03:15 PM #3
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:
Not sure if that is the best way of doing it, but it should work. s4 should contain the expected result.Java Code:String[] s4 = " 1 10.25 3.3 0.32 ".trim().split("\\s+");
EDIT: Fixed the code a littleLast edited by OutputStream; 04-19-2011 at 03:40 PM.
Similar Threads
-
Convert String to Date?
By bochra in forum New To JavaReplies: 4Last Post: 11-15-2010, 10:41 AM -
convert obj to string
By isme in forum New To JavaReplies: 11Last Post: 06-14-2010, 10:54 AM -
How to convert a String into an Hexadecimal ?
By ze snow in forum New To JavaReplies: 7Last Post: 02-16-2010, 10:31 PM -
how to convert a string to an integer
By LAW in forum New To JavaReplies: 6Last Post: 11-09-2009, 03:29 AM -
convert a really big string (len 39) to bigdecimal
By coolsig in forum Advanced JavaReplies: 6Last Post: 06-14-2008, 02:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks