Home > Enterprise >  How can I convert a sting arrray to float and sum the valus inside it? Java
How can I convert a sting arrray to float and sum the valus inside it? Java

Time:04-20

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);
            }
        }

CodePudding user response:

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);
  • Related