0

I have this code to read each line of a file of this type "603,The Matrix,1999-03-30,63000000,136,7.9,9079" but I only need to read the first 3 parameters and the 3rd one each is a date needs to be read separately, therefor it needs to put the year in a var and the month in other var and then the day in another var but the output I get is this: "[603 | The Matrix | 03-603,The Matrix,1999-1999-03-30"

 int i; Scanner leitorFicheiroFilmes = new Scanner(ficheiroFilmes); ArrayList<Filmes> filme = new ArrayList<>(); for (i = 0; leitorFicheiroFilmes.hasNextLine(); i++) { String line = leitorFicheiroFilmes.nextLine(); String dados[] = linha.split(","); if (dados.length == 7) { int idFilme = Integer.parseInt(dados[0]); String titulo = dados[1]; String dadosNew[] = line.split("-"); String ano = dados[2]; String mes = dadosNew[0]; String dia = dadosNew[1]; filme.add(new Filmes(idFilme, title, year, month, day, parseActoresFile(), parseGenerosFile(idFilme))); } } 

this is the class with the constructor:

 public class Filmes { int idFilme; String titulo; ArrayList<Actores> actores = new ArrayList<Actores>(); ArrayList<GenerosCinematograficos> generos = new ArrayList<GenerosCinematograficos>(); String year, month, day; public Filmes(int idFilme, String titulo, String year, String month, String day, ArrayList<Actores> actores, ArrayList<GenerosCinematograficos> generos) { this.idFilme = idFilme; this.titulo = titulo; this.year = year; this.month = month; this.day = day; this.actores = actores; this.generos = generos; } public String toString() { return idFilme + " | " + titulo + " | " + dia + "-" + mes + "-" + ano; } } 
1
  • I would use a CSV parser. If you have a choice on what to use, I would also recommend Python's pandas package or R to deal with this sort of data. Java isn't entirely the best tool for this. However that is, stepping this through a debugger would almost certainly solve your problem trivially. Commented May 30, 2018 at 16:34

3 Answers 3

1
String dadosNew[] = line.split("-"); 

must be

String dadosNew[] = dados[2].split("-"); 

dadosNew array will be [1999,03,30] from which you can get the date, month and year by accessing the correct indices.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you, it's working now has it should, Im new to Java so I didn't knew you could do that.
0

You are reading incorrect values to your variables when parsing the date

String dadosNew[] = line.split("-"); String ano = dados[2]; String mes = dadosNew[0]; String dia = dadosNew[1]; 

to

String dadosNew[] = dados[2].split("-"); String ano = dadosNew[0]; String mes = dadosNew[1]; String dia = dadosNew[2]; 

Comments

0

The problem is here :

String dadosNew[] = line.split("-"); 

With the input (line) being "603,The Matrix,1999-03-30,63000000,136,7.9,9079" The result wille be :

{"603,The Matrix,1999", "03", "30,63000000,136,7.9,9079"} 

You want to split only the date, and this is contained in dados[2], so to correct it you have to do :

String dadosNew[] = dados[2].split("-"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.