Inserting Selected Objects in Array

3

I'm doing a Formula 1 Racing program, my program reads various racing files, and creates the test objects. I need to deploy in this program a class that has an array to store only the Pilots that have points above 0, and if the pilot is already in the array, their score must be updated, but the objects are not being created in the array: / p>

Part of the main class:

public class Controle {
    public static void main(String[] args) {

        Temporada2013 temporada = new Temporada2013();
        File arquivos[];
        File diretorio = new File("/Users/leonardobruksch/NetBeansProjects/Corridas");
        arquivos = diretorio.listFiles();

        for(int a=0;a<arquivos.length;a++){
            Provas prova = new Provas();
        try {
            FileReader fr = new FileReader(arquivos[a]);
            BufferedReader in = new BufferedReader(fr);
            String line = in.readLine();
            line = in.readLine();
            while (line != null) {
                String result[] = line.split(";");
                int x = Integer.parseInt(result[6]);
                prova.inserePiloto(result[0], result[1], result[2], result[3], result[4], result[5], x);
                if(x > 0)
                    temporada.inserePilotosPontuados(result[1], result[2], x);
                line = in.readLine();
            }

Part of the class Season2013 that performs the insertion:

public class Temporada2013 {

    Piloto[] pontuados = new Piloto[30];

    public void inserePilotosPontuados(String driver, String team, int points) {
        for(int i=0;i<getLast();i++){
            if(pontuados[i].getDriver().equalsIgnoreCase(driver)){
                pontuados[i].setPoints(pontuados[i].getPoints()+points);
                return;
            }

        }
        pontuados[getLast()] = new Piloto(driver, team, points);
    }

    private int getLast(){
        int last = 0;
        for(int i=0;i<pontuados.length;i++){
            if(pontuados[i] == null)
                last = i;
                return last;
        }
        return last;
    }

What is happening is that the above method, after being executed and displayed, is creating only one object in the array, instead of creating several.

    
asked by anonymous 16.09.2014 / 18:09

1 answer

5

I believe the problem is here:

private int getLast(){
    int last = 0;
    for(int i=0;i<pontuados.length;i++){
        if(pontuados[i] == null)
            last = i;
            return last;
    }
    return last;
}

As you can see, if does not have keys {} . Keys are used to delimit blocks of code. Omitting the keys, you just make the statement after if become part of it.

In short ... by removing the keys, you remove the ability of your if to have multiple instructions.

Explaining with the code is easier. See the snippet of your code below:

int last = 0;

for(int i=0;i<getLast();i++){
    if(pontuados[i] == null)
        last = i;
        return last;
}

Doing this would be the same thing to do:

int last = 0;

for(int i=0;i<getLast();i++){
    if(pontuados[i] == null) {
        last = i;
    }

    return last;
}

So, the for above will return last soon in the first iteration. As last = 0 , your method will return 0 always. Return 0 will cause a side effect on inserePilotosPontuados method, where:

pontuados[getLast()] = new Piloto(driver, team, points);

It's the same thing to do:

pontuados[0] = new Piloto(driver, team, points);

The code below should work without problems:

private int getLast() {
    int last = 0;

    for (int i = 0; i < pontuados.length; i++) {
        if (pontuados[i] == null) {
            last = i;
            return last;
        }
    }

    return last;
}
    
16.09.2014 / 18:50