How do I resolve the Arrayindexofbox exception in this program?

Asked

Viewed 62 times

0

package projecto_algoritmia;
import java.util.Scanner;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;

/**
 *
 * @author Jeje
 */





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 {
            File ficheiro=new File(nomeFicheiro);
            Scanner scanner = new Scanner(ficheiro);

                while (scanner.hasNextLine()) {

                   String linha = scanner.nextLine();
                   String dados[] = linha.split(" : ");


                    System.out.println("*");
                    Musica musica= new Musica(dados[0],Integer.parseInt(dados[1]),Integer.parseInt(dados[2]),Integer.parseInt(dados[3]),Integer.parseInt(dados[4]));


                   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);
}
}

public class Musica {
    String titulo;
    Integer id_interprete, ano, id_genero, posicao;

    public Musica(String titulo, Integer id_interprete, Integer ano, Integer id_genero, Integer posicao) {
        this.titulo = titulo;
        this.id_interprete = id_interprete;
        this.ano = ano;
        this.id_genero = id_genero;
        this.posicao = posicao;
    }

    Musica() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

I’m reading a file where each line represents data from a song I want to save to the Music object

I am having the error of Arrayindexofbox and do not understand the cause.

The file to be read is:

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
  • ArrayIndexofBox or ArrayIndexOutOfBounds???

  • ofBounds. Thank you

  • 1

    thanks guys, I managed to solve the problem

No answers

Browser other questions tagged

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