繁体   English   中英

读取文本文件并制作2D数组

[英]Read text file and make a 2D array

我想读取一个文本文件并从中创建一个2D数组,但我对2D数组并不熟悉。 因此,任何帮助都非常感激我的文本文件包含所有数字。

我也尝试了for循环,但是却遇到了同样的错误。

请在这里查看我的代码工作量

  Scanner readFile = new Scanner(new File("ELEVATIONS.txt"));
            rowLength = readFile.nextInt();
            colLength = readFile.nextInt();
            Radius = readFile.nextInt();

            int[][] data = new int[rowLength][colLength];

            System.out.println("ROW : " + rowLength + " COl : " + colLength + " Radius : " + Radius );

            int row = 0;
            while (readFile.hasNextInt() && row < data.length) {
                for (int col = 0; col < colLength; col++) {
                    data[row][col]= readFile.nextInt();
                }
                row++;
            }

当我运行代码时,它显示以下错误

ROW:1000 COl:450 Radius:10数据1000450线程“主”中的异常java.util.Scanner.throwFor(Scanner.java:862)处java.util.Scanner.next(Scanner.java: 1485)在java.util.Scanner.nextInt(Scanner.java:2117)在java.util.Scanner.nextInt(Scanner.java:2076)在lab1.Lab1.main(Lab1.java:43)

请帮我。

我认为最简单的方法是从文件中读取所有整数并使用计数器填充2D数组:

public static int[][] readFile(File file) throws FileNotFoundException {
    try (Scanner scan = new Scanner(file)) {
        int rows = scan.nextInt();
        int cols = scan.nextInt();

        int[][] res = new int[rows][cols];

        for (int row = 0, col = 0; row < rows; ) {
            res[row][col++] = scan.nextInt();

            if (col == cols) {
                row++;
                col = 0;
            }
        }

        return res;
    }
}

您的“ 2D数组”代码看起来不错,您准备了“空”数组,然后根据其尺寸进行循环。

您问题的实质体现在这里:

java.util.Scanner.throwFor(Scanner.java:862)处的java.util.NoSuchElementException

重点是: 读取文件时,您最终会遇到错误。 无论您是仅将值打印到控制台,还是将它们存储在该预定义的数组中,这都无关紧要。

换句话说:在这一点上,你的问题是,你的文件内容符合您的期望/假设。 因此,您要求输入一个数字……在没有数字的地方。

答案:退后一步。 从一个很小的文件开始,该文件仅包含2 2 5 1 2 3 4 现在,使用该代码运行您的代码,然后打印所有相关信息(例如,从文件中读取的值)。 使您了解您的代码到底在做什么。

换句话说:需要调试。 通过实际使用调试器或添加打印语句。 您需要能够观察您的代码完成其工作。

您得到的错误主要是因为您的文本文件不包含您的代码试图获取的元素。

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

public class Test {
    public static void main(String[] args) throws Exception {
        // pass the path to the file as a parameter
        File file =

                new File("C:\\Users\\LENOVO\\Documents\\test.txt");
        Scanner sc = new Scanner(file);
        int rowLength = 0;
        int colLength = 0;
        int radius = 0;

        rowLength = sc.nextInt();
        colLength = sc.nextInt();
        radius = sc.nextInt();

        int[][] data = new int[rowLength][colLength];
        System.out.println("ROW : " + rowLength + " COl : " + colLength + " Radius : " + radius);

        for (int i = 0; i < rowLength; ++i) {
            for (int j = 0; j < colLength; ++j) {
                if (sc.hasNextInt()) {
                    data[i][j] = sc.nextInt();
                }
            }
        }

        // printing the array
        for (int i = 0; i < rowLength; i++) {
            for (int j = 0; j < colLength; j++) {
                System.out.print(data[i][j] + " ");
            }
            System.out.println();
        }
    }
}

在此处输入图片说明

在此处输入图片说明 我的文本文件内容-2 2 5 1 2 3 4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM