-1

I got this array of values

float Guy_A [] =  {194,180,221,432,820,200,312,177,190, 70};

And i wrote this "for" for a total values

for (int i=0; i < Guy_A.length;i++)
    { A += Guy_A[i];}

And after that i want to make a mid point divition using this

float pcnt_a = A * 100 /vt;
//the vt is declared above as a float and it value is 10489

After that i run the file using this commands:

System.out.println("Guy A has "+A+" votes");
System.out.println("His Average is"+df.format(pcnt_a)+" %\n");

The problem is that the first line returns '2796.0' (the actual total) but the second one only returns 0.00 '%' //the df.format is 0.00, for skip other 6 decimal numbers.

I have tried with double and int, but it still shows 0.0. Can some one please explain me what can i do with this thing...

//Full code:

public class Estadisticas { //float A = 2796;

    float Candidato_A [] =  {194,180,221,432,820,200,312,177,190, 70};
    float Candidato_B [] = { 48, 20, 90, 50, 61,100, 99, 87, 77,122};
    float Candidato_C [] = {206,320,140,821,946,155,501,075,005,12};
    float Candidato_D [] = { 45, 16, 20, 14, 18,355,122,178,501,117};
    float Candidato_E [] = {155,501,075,178,501,117,700,55,78,12};
    float vt = 10489;
    boolean no_win;

    public void totalVotos(){
        for (int i=0; i<Candidato_A.length;i++){
            A += Candidato_A[i]; }
        for (int i=0; i<Candidato_B.length;i++){
            B += Candidato_B[i]; }
        for (int i=0; i<Candidato_C.length;i++){
            C += Candidato_C[i]; }
        for (int i=0; i<Candidato_D.length;i++){
            D += Candidato_D[i]; }
        for (int i=0; i<Candidato_E.length;i++){
            E += Candidato_E[i];
        }
    }

    float A; float B; float C; float D; float E;

    float pcnt_a = A * 100 /vt;
    float pcnt_b = B * 100 /vt;
    float pcnt_c = C * 100 /vt;
    float pcnt_d = D * 100 /vt;
    float pcnt_e = E * 100 /vt;

    public void imprimeDatos(){
        DecimalFormat df= new DecimalFormat("0.00");
        System.out.println("\n\n -- Estadisticas de las elecciones --\n");
        System.out.println("El Candidato A obtuvo "+A+" votos");
        System.out.println("Su Porcentaje de votos fue de "+df.format(pcnt_a)+" %\n");       System.out.println("El Candidato B obtuvo "+B+" votos");
        System.out.println("Su Porcentaje de votos fue de"+df.format(pcnt_b)+" %\n");
        System.out.println("El Candidato C obtuvo"+C+" votos");
        System.out.println("Su Porcentaje de votos fue de"+df.format(pcnt_c)+" %\n");
        System.out.println("El Candidato D obtuvo"+D+" votos");
        System.out.println("Su Porcentaje de votos fue de"+df.format(pcnt_d)+" %\n");
        System.out.println("El Candidato E obtuvo"+E+" votos");
        System.out.println("Su Porcentaje de votos fue de"+df.format(pcnt_e)+" %\n");

        if(A>B && A>C && A>D && A>E){
             System.out.println("-- El Candidato A obtuvo la mayor cantidad de votos --\n");
        } else{
            if(B>A && B>C && B>D && B>E){
                System.out.println("-- El Candidato B obtuvo la mayor cantidad de votos --\n");
            } else{
                if(C>B && C>A && C>D && C>E){
                    System.out.println("-- El Candidato C obtuvo la mayor cantidad de votos --\n");
                } else{
                    if(D>B && D>C && D>A && D>E){
                        System.out.println("-- El Candidato D obtuvo la mayor cantidad de votos --\n");
                    } else{
                        if(E>B && E>C && E>D && E>A){   
                            System.out.println("-- El Candidato E obtuvo la mayor cantidad de votos --\n");
                        }
                    }
                }
            }
        }
    }

Sorry for the spanish, right now i need this for a university proyect.

Radiodef
  • 37,180
  • 14
  • 90
  • 125

3 Answers3

1

It looks like A and pcnt_a are both fields. A is 0 at the time pcnt_a is set as A * 100 /vt. You need to calculate pcnt_a after running the sum loop where A is calculated.

public void totalVotos() {
    for (int i = 0; i < Candidato_A.length; i++) {
        A += Candidato_A[i];
    }
    for (int i = 0; i < Candidato_B.length; i++) {
        B += Candidato_B[i];
    }
    for (int i = 0; i < Candidato_C.length; i++) {
        C += Candidato_C[i];
    }
    for (int i = 0; i < Candidato_D.length; i++) {
        D += Candidato_D[i];
    }
    for (int i = 0; i < Candidato_E.length; i++) {
        E += Candidato_E[i];
    }
    pcnt_a = A * 100 / vt;
    pcnt_b = B * 100 / vt;
    pcnt_c = C * 100 / vt;
    pcnt_d = D * 100 / vt;
    pcnt_e = E * 100 / vt;
}
Radiodef
  • 37,180
  • 14
  • 90
  • 125
  • Also as a side note, OP I hope your actual code looks nothing like the formatting nightmare that was your code when it was originally posted. If that was not just a copy and paste issue then no wonder you are having little problems like this. Readable code formatting is seriously one of the most important things. And @DennisMeng thanks. – Radiodef Nov 18 '13 at 02:13
  • if i move pcnt_ltr to another method it will works?. – user3002972 Nov 18 '13 at 02:14
  • and nope, i thought it was a float X int thing, but i will copy code everytime, thnx again – user3002972 Nov 18 '13 at 02:20
  • No problem! You're welcome. And if it's the answer make sure to mark the question as solved. – Radiodef Nov 18 '13 at 02:31
0

you didn't mention the value you are assigning while instantiating df. Try this: DecimalFormat df = new DecimalFormat("####.00");

farhanjk
  • 1,652
  • 16
  • 17
0

Is your formatter is wrong? Insure that your format string in your formatter(I assume df means decimal formatter) actually indicates the number of zeros, not just the string "0.0". I don't know what DF is though.

awiebe
  • 3,758
  • 4
  • 22
  • 33