Java文件读取解析日期时输出格式异常问题排查
Fixing Your Java File Parsing & Output Error
Let's walk through the bugs in your code and fix them to get the correct output:
Key Issues Identified
- Variable Name Typo: You used
linha.split(",")but the variable holding the line is namedline(a mix-up of Portuguese/English naming). - Incorrect Date Splitting: You're splitting the entire
linewith-instead of targeting the third field (dados[2]) which contains the date string. Splitting the whole line leads to garbage values like03-603in your output. - Mismatched Variables: Inconsistent variable names (e.g., using
titleinstead oftitulo, assigning the full date string toanoinstead of splitting it into year/month/day) caused wrong data to be stored. - Broken
toString(): The method references non-existent fields (dia,mes,ano) instead of the class's actualyear,month,dayfields, and uses the wrong date order.
Corrected Code
Main Parsing Logic
int i; Scanner leitorFicheiroFilmes = new Scanner(ficheiroFilmes); ArrayList<Filmes> filme = new ArrayList<>(); for (i = 0; leitorFicheiroFilmes.hasNextLine(); i++) { String line = leitorFicheiroFilmes.nextLine(); String dados[] = line.split(","); // Fixed typo: linha -> line if (dados.length == 7) { int idFilme = Integer.parseInt(dados[0]); String titulo = dados[1]; // Split ONLY the date field (dados[2]) instead of the entire line String dateParts[] = dados[2].split("-"); if (dateParts.length == 3) { // Add check to avoid invalid date formats String year = dateParts[0]; String month = dateParts[1]; String day = dateParts[2]; // Align variable names with Filmes constructor filme.add(new Filmes(idFilme, titulo, year, month, day, parseActoresFile(), parseGenerosFile(idFilme))); } } }
Corrected Filmes Class
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; } @Override public String toString() { // Use correct class fields and standard date order (YYYY-MM-DD) return idFilme + " | " + titulo + " | " + year + "-" + month + "-" + day; } }
Explanation of Fixes
- Fixed Variable Typo: Changed
linha.split(",")toline.split(",")to match the actual variable name. - Targeted Date Splitting: Now we split
dados[2](the dedicated date field) instead of the entire line, so we correctly extractyear,month,dayfrom1999-03-30. - Consistent Variable Names: Aligned all variable names with the
Filmesclass constructor and fields to ensure data is assigned to the right places. - Corrected
toString(): Updated the method to use the class's actualyear,month,dayfields and output the date in a clean, standard format. You'll now get output like603 | The Matrix | 1999-03-30.
内容的提问来源于stack exchange,提问作者Jorge
相关产品推荐
相关产品推荐

