Reputation: 497
I have a string with about 150 numbers like this String num = "64513246563........";
I am trying to add each digit of this string. So my idea was to split it into an array of ints and add them from there. I start by splitting it into a String array, then I try to convert it to an Int array. I get an unknown source error. Below is the code:
String[] strArray = num.split("");
int[] intArray = new int[strArray.length];
for(int i = 0; i < strArray.length; i++) {
intArray[i] = Integer.parseInt(strArray[i]);
}
And here is the error:
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)
Can anyone see what I'm doing wrong or is there a more efficient way of doing this?
////////////////////////////////
Thanks for your help everyone, it seems that splitting a string using .split("") creates an empty string at index 0. That was my main problem but there was lots of useful pointers on how to solve the problem more efficiently :) Thank you all for your input
Upvotes: 3
Views: 7022
Reputation: 15844
int sum = 0;
for (int i = 0; i < string.length(); i++) {
sum += Character.getNumericValue(string.charAt(i));
}
Upvotes: 5
Reputation: 6572
Since array start from a empty string you have to skip first element hence Start i
from 1
in your for loop.
for(int i = 1; i < strArray.length; i++)
Upvotes: 0
Reputation: 46408
String num="12234";
String[] numarr = num.split("");
System.out.println(numarr.length);
When you split a string with ""
there will be an empty string at the zeroth index.
Thus your resulting array would be :
{"", "1", "2", "2", "3", "4"}
test it, the above returns the length as 6. thus Integer.parseInt("")
leads to NumberFormatException.
Test:
String num="12234";
String[] numarr = num.split("");
for(String s: numarr){
if(s.equals("")){
System.out.println("empty string");
}
else {
System.out.println(s);
}
Output:
empty string
1
2
2
3
4
Thus, there is an emptyString at index 0.
so, you will have to check if the String is an empty string before parsing it:
String[] strArray = num.split("");
int[] intArray = new int[strArray.length];
for(int i = 0; i < strArray.length; i++) {
if(!strArray[i].equals(""))
intArray[i] = Integer.parseInt(strArray[i]);
}
Upvotes: 2
Reputation: 66637
java.lang.NumberFormatException: For input string: ""
exception message clearly telling that input is empty String.
One way to handle would be, do empty String
check before parsing.
for(int i = 0; i < strArray.length(); i++) {
if(strArray[i] != null && !"".equals(strArray[i])
{
int q = Integer.parseInt(strArray[i]);
}
}
Note: This will still fail if input is not valid int, but not in case of empty String (or) null.
Another way to handle this would be get chartAt(i)
instead of using split("")
Upvotes: 3
Reputation: 234795
I'd simply step along the string, parsing each digit:
int sum = 0;
for (int i = 0; i < num.length(); ++i) {
sum += Integer.parseInt(num.substring(i, i + 1));
}
Upvotes: 0
Reputation: 24334
char[] charArray = num.toCharArray();
for(char c : charArray) {
int q = Integer.parseInt("" + c);
}
Upvotes: 1
Reputation: 382150
The first string given by num.split("");
is empty.
A solution is to simply start the iteration at index 1.
But what you do is much too expensive as you could do
String num = "64513246563";
for(int i = 0; i < num.length(); i++) {
int digit = num.charAt(i)-'0';
... // use the digit, perhaps adding it
}
Upvotes: 8
Reputation: 5447
for(int i=0; i<yourString.length; i++) {
yourIntegers[i] = Integer.parseInt(yourString.charAt(i).ToString());
}
Upvotes: 0