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

使用Java Scanner类读取TXT数据写入二维数组失败问题

问题排查与解决

核心问题分析

  • Scanner指针已耗尽:第一次统计行数时,file1和file2的Scanner已经把文件内容全部读完,指针停在文件末尾,后续调用file1.hasNextLine()直接返回false,插入数据的循环完全没执行,这是数组全为null的直接原因。
  • 二维数组子数组未初始化:int[][] combinedArray = new int[totalRow][];只初始化了外层数组的长度,每个子数组都是null,就算能读到数据,直接赋值也会触发NullPointerException。
  • 读取逻辑完全错误:用file1.nextLine().length()判断一行的数字个数完全不成立——这是取整行字符串的字符长度,不是数字数量;同时重复调用nextLine()和nextInt()会导致读取位置错位,跳过有效数据。

修复步骤

  1. 重新创建Scanner读取数据:统计完行数后,原Scanner已经到文件末尾,重新创建Scanner对象来读取文件内容。
  2. 先处理每行数据再初始化子数组:读取每行内容,按分隔符(比如空格)拆分出所有数字,转成int数组后直接赋值给combinedArray的对应位置,无需提前计算子数组长度。
  3. 统一读取逻辑:用nextLine()读取整行,再拆分处理,避免nextInt()和nextLine()混用导致的指针偏移问题。

完整修复代码

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        // 第一次统计行数
        Scanner file1Count = new Scanner(new File("C:\\Users\\admin\\IntellijProjects\\AlgorithmsProblemSolving2\\numbers1.txt"));
        Scanner file2Count = new Scanner(new File("C:\\Users\\admin\\IntellijProjects\\AlgorithmsProblemSolving2\\numbers2.txt"));

        int rowFile1 = 0;
        while (file1Count.hasNextLine()) {
            rowFile1++;
            file1Count.nextLine();
        }
        int rowFile2 = 0;
        while (file2Count.hasNextLine()) {
            rowFile2++;
            file2Count.nextLine();
        }
        // 关闭统计用的Scanner
        file1Count.close();
        file2Count.close();

        int totalRow = rowFile1 + rowFile2;
        int[][] combinedArray = new int[totalRow][];

        // 重新创建Scanner读取数据
        Scanner file1 = new Scanner(new File("C:\\Users\\admin\\IntellijProjects\\AlgorithmsProblemSolving2\\numbers1.txt"));
        Scanner file2 = new Scanner(new File("C:\\Users\\admin\\IntellijProjects\\AlgorithmsProblemSolving2\\numbers2.txt"));

        // 读取file1的数据到combinedArray
        int currentRow = 0;
        while (file1.hasNextLine()) {
            String line = file1.nextLine().trim();
            // 跳过空行
            if (line.isEmpty()) continue;
            // 按空格拆分数字字符串,转成int数组
            String[] numStrs = line.split("\\s+");
            int[] nums = Arrays.stream(numStrs).mapToInt(Integer::parseInt).toArray();
            combinedArray[currentRow++] = nums;
        }

        // 读取file2的数据到combinedArray
        while (file2.hasNextLine()) {
            String line = file2.nextLine().trim();
            if (line.isEmpty()) continue;
            String[] numStrs = line.split("\\s+");
            int[] nums = Arrays.stream(numStrs).mapToInt(Integer::parseInt).toArray();
            combinedArray[currentRow++] = nums;
        }

        // 关闭读取用的Scanner
        file1.close();
        file2.close();

        System.out.println(Arrays.deepToString(combinedArray));
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 15:16:17