'How can I convert a sting arrray to float and sum the valus inside it? Java
I have string array with numeric vaues and i wont to convert them to float. My problem is that i can convert them and print thhem out but I cant sum them. Can anyone tell me how to fix that problem,
for(int j=0; j<=val.length-1;j++){
float valf = Float.parseFloat(val[j]);
if(valf!=0){
System.out.println(valf);
}
}
Solution 1:[1]
You can declare a sum variable before entering the for loop. For example:
float sum = 0;
for(int j=0; j<=val.length-1;j++){
float valf = Float.parseFloat(val[j]);
if(valf!=0){
System.out.println(valf);
sum += valf;
}
}
System.out.println("Sum: "+sum);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
