Bom dia pessoal. o meu projecto e ler um arquivo de musicas, cada linha e uma musica, e os dados da musica estao separados por “:”, no entanto nao entendo o porque de estar a dar o erro de array index of box.
class Musica{
String titulo;
Integer id_interprete, ano, id_genero, posicao;
}
class interprete{
int id;
String nome;
}
public class Projecto_Algoritmia {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String id_inteprete, ano, id_genero, posicao;
String nomeFicheiro = "musicas.txt";
try {
Scanner scanner = new Scanner(new FileReader("musicas.txt"));
while (scanner.hasNextLine()) {
String ler[]=scanner.nextLine().replace(" : ", ":").replace(": ",":").replace(" :",":").trim().split(":");
if(ler!=null){
System.out.println("*");
Musica musica= new Musica();
musica.titulo =ler[0]!=null? ler[0] : "";
id_inteprete = ler[1]!=null? ler[1] : "";
ano = ler[2]!=null? ler[2] : "";
id_genero= ler[3]!=null? ler[3] : "" ;
posicao = ler[4]!=null? ler[4] : "" ;
musica.id_interprete=(Integer.parseInt(id_inteprete));
musica.ano=(Integer.parseInt(ano));
musica.id_genero=(Integer.parseInt(id_genero));
musica.posicao=(Integer.parseInt(posicao));
System.out.println(musica.titulo+" "+musica.id_interprete+" "+musica.ano+" "+musica.id_genero+" "+musica.posicao);
}
}
scanner.close();
}
catch(FileNotFoundException exception) {
String mensagem = "Erro: o ficheiro " + nomeFicheiro + "nao foi encontrado.";
System.out.println(mensagem);
}
}
o arquivo a ser lido e:
Amar pelos dois : 0 : 2017 : 1 : 0
Epic : 5 : 1990 : 3 : 1
The Rockafeller Skank : 6 : 1998 : 6 : 12
I Think Im Paranoid : 3 : 1998 : 3 : 3
O meu coracao nao tem cor : 12 : 1996 : 7 : 4
Boulevard of Broken Dreams : 9 : 2004 : 3 : 8
Chamar a musica : 11 : 1996 : 7 : 6
From Here to Eternity : 1 : 1977 : 6 : 2
Racer : 1 : 2013 : 6 : 5
Get Lucky : 13 : 2013 : 6 : 7
Take my breath away : 10 : 1986 : 2 : 9
Nightcall : 7 : 2010 : 6 : 10
Danger Zone : 4 : 1986 : 7 : 11
Nothing Compares 2U : 2 : 1990 : 2 : 13
Ch-Check it Out : 14 : 2004 : 7 : 14
Best of My Love : 18 : 1977 : 7 : 16
Hooked on a Feeling : 20 : 1974 : 2 : 18
Rolling in the deep : 17 : 2010 : 7 : 15
Cold as Ice : 8 : 1977 : 3 : 19
Best of My Love : 19 : 1974 : 3 : 17
conto com a vossa ajuda.