You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 named line (a mix-up of Portuguese/English naming).
  • Incorrect Date Splitting: You're splitting the entire line with - instead of targeting the third field (dados[2]) which contains the date string. Splitting the whole line leads to garbage values like 03-603 in your output.
  • Mismatched Variables: Inconsistent variable names (e.g., using title instead of titulo, assigning the full date string to ano instead 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 actual year, month, day fields, 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

  1. Fixed Variable Typo: Changed linha.split(",") to line.split(",") to match the actual variable name.
  2. Targeted Date Splitting: Now we split dados[2] (the dedicated date field) instead of the entire line, so we correctly extract year, month, day from 1999-03-30.
  3. Consistent Variable Names: Aligned all variable names with the Filmes class constructor and fields to ensure data is assigned to the right places.
  4. Corrected toString(): Updated the method to use the class's actual year, month, day fields and output the date in a clean, standard format. You'll now get output like 603 | The Matrix | 1999-03-30.

内容的提问来源于stack exchange,提问作者Jorge

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:23:47