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
?– gabrielhof
Yes, because in some cases the variable x(result[6]) which was transformed to integer, and greater than 0.
– leonardokbruksch