开发者

java : get values from table and apply formula

in the bellow code, I try to calcul the formula:

y=val1+开发者_JS百科val2+val4/all values

val is a string get from a table.

my aim is , for a row 0, get all values from each column "values" then calcul the formule.

after that do the same for each row. but my code doesn't print me the expected behaviour for the first step.

thanks,

>     String[] values = { x1,x2,x3,x4};
>         
>         String val = null ;
>         for (int i = 0; i < values .length; i++)
>         {
>            val = table.getValue(0, table.getColumnValue(x[i]));
>             
>         }
> 
> //my fomula y = value[x[0]]+value[x[1]]+value[x[3]]/values[0..3]
> 
> int Num =  Integer.parseInt(value[x[0]])+Integer.parseInt(value[x[1]])+Integer.parseInt(value[x[3]]); 
> int Denum =  Integer.parseInt(val );
> 
> y=Num/Denum ;


are you sure that variable value shouldn't be values in this code?


"1" + "2" = "12" //string
1 + 2 = 3 //int

You cannot add String, have to convert them to number first:

int[] valuesInt = new int[values.length];
for (int i = 0; i < values .length; i++) {
    valuesInt[i] = Integer.parseInt(values[i]);
}

Then add the numbers:

int Num =  valuesInt[0]+valuesInt[1]+valuesInt[3];
int Denum =  valuesInt[0]+valuesInt[1]+valuesInt[2]+valuesInt[3];

Calcaulate Denum in the loop:

int Denum = 0; 
for (int i = 0; i < values .length; i++) {
    Denum += Integer.parseInt(values[i]);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜