string = "#1x#2#3#4#5#6#";
String array[] = string.split("#");
for(int count=0;count<=6;count++){
// String temp = array[count];
String temp1 = array[count].substring(0,1).trim();//here temp1="1"
// String temp1 = "1"; //this is possible
log (Integer.parseInt( temp1));// i cant convert the array which is populated by split("delimiter") why ??
}
Here’s the array:
array[]=("1","2","3","4","5","6"}
I can convert the the String array[] into integer when i am declaring this as normal String array but when i am using split and populating the String array here in this example .
I am ready to explain if you can’t understand too. Help me out in this?
The Error is
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
My question is Integer.parseInt(string_array_populated_by_split_method[count]) is throwing Exception
Here the return value is a string “1” i.e Integer.parseInt(“1”).. this “1” is generated by split() method
Thanks in Advance.
You are doing a
which is returning
"", and""cannot be converted to int. If you want to get the first character, you need to do:Then why you are trimming a single character, I don’t know…
Edit
Your new problem is:
The fact that your String starts with a
#means that the first value in your array will be a"", and this one is causing problem. What you can do is testing that the String length is superior that 0:Or you could also make sure that your String at the beginning isn’t starting with a
#.