Insert Selected Objects into Array

Asked

Viewed 136 times

3

I’m doing a Formula 1 racing program, my program performs reading of various race files, and creates the objects of the race. I need to deploy in this program a class that has an array to store only Pilots who have points above 0, and if the pilot is already in the array, their score should be updated, however, the objects are not being created in the array:

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 Temporada2013 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 occurring is that the above method, after it is executed and displayed, is creating only one object in the array, rather than creating several.

  • The code even enters the method temporada.inserePilotosPontuados?

  • Yes, because in some cases the variable x(result[6]) which was transformed to integer, and greater than 0.

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, the if did not have keys {}. Keys are used to delimit blocks of code. Omitting keys, you make only the instruction after the if be part of it.

In short... by removing the keys, you remove the ability of your if to have several 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;
}

To do that would be the same as to do:

int last = 0;

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

    return last;
}

Thus the for above will return last in the first iteration. How last = 0, your method will return 0 at all times. Return 0 will cause a side effect on the method inserePilotosPontuados, where:

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

It’s the same as doing:

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

The code below should work smoothly:

private int getLast() {
    int last = 0;

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

    return last;
}
  • Yes Gabriel!! That would be the problem, it was a stupid mistake and caused by the lack of attention, but what happens! hahaha worth!

  • 1

    Hehe. But look at this, I recommend you use {} at all times. I know that, depending on the point of view, it can cost a line of code, make the code "uglier", etc. But it makes the code more readable and less susceptible to errors of this type.

  • Great answer, I confess I lost a little time and I didn’t notice. A good question for me is when the array is complete, how this function behaves?

  • I believe you will return 0 also.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.