繁体   English   中英

Java-将输入文件中的数字存储在2D数组中

[英]Java - Storing numbers from input file in a 2D array

该程序的目标是从输入的文本文件中获取数字,该文件名为seatPrices.txt ,并将其保存在double类型的数组中,该数组称为priceArray

输入文件为:

310:300:320:320:300:310
360:350:370:370:350:360
360:350:370:370:350:360
360:350:370:370:350:360
360:350:370:370:350:360
260:250:270:270:250:260
260:250:270:270:250:260
260:250:270:270:250:260
260:250:270:270:250:260
260:250:270:270:250:260
210:200:220:220:200:210
210:200:220:220:200:210
210:200:220:220:200:210
210:200:220:220:200:210
210:200:220:220:200:210
210:200:220:220:200:210
210:200:220:220:200:210
210:200:220:220:200:210
210:200:220:220:200:210
210:200:220:220:200:210
110:100:120:120:100:110
110:100:120:120:100:110
110:100:120:120:100:110
110:100:120:120:100:110
110:100:120:120:100:110
110:100:120:120:100:110
110:100:120:120:100:110
110:100:120:120:100:110
110:100:120:120:100:110
110:100:120:120:100:110

我的代码是:

import java.io.FileReader;
import java.util.Scanner;
import java.io.IOException;

public class testAirline {

    // initializing variables and arrays
    private static final int MAX_COLUMN = 6;
    private static final int MAX_ROW = 30;
    double[][] priceArray;
    FileReader fr;
    Scanner scan;

    // this constructor reserves memory for arrays and sets the filereader and scanner variables, then call "start" method
    public void Airline() throws IOException {      
        priceArray = new double[MAX_ROW][MAX_COLUMN];
        fr = new FileReader(seatPrices);
        scan = new Scanner(fr).useDelimiter(":");
        start();
    }

    // fills the priceArray with numbers from seatPrices.txt
    private void start() {
        for (int i = 0; i < MAX_ROW; i++) {
            for (int j = 0; j <  MAX_COLUMN; j++) {
                priceArray[i][j] = scan.nextDouble();
            }
        }
    }
}

运行此命令时,在控制台中出现以下错误:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextDouble(Scanner.java:2413)
    at testAirline.start(Airline.java:19)
    at testAirline.Airline(Airline.java:12)

因此,当我调用“ start”方法( start() )并从文件中扫描数字以将其存储在数组中时,错误似乎就来自此。 我不知道为什么会抛出这些错误。 由于我是使用delimiter新手,所以我认为这些问题可能是由于格式不正确而引起的,但是当我发现错误在代码中的哪几行抛出时,这种可疑性就被消除了。

我在这里找不到在我的程序中起作用的答案。 我愿意接受所有可能的解决方案! 任何和所有帮助表示赞赏!

编辑:

@robert建议代码的输出:

:0,0 : 310.0
:0,1 : 300.0
:0,2 : 320.0
:0,3 : 320.0
:0,4 : 300.0
:1,0 : 360.0
:1,1 : 350.0
:1,2 : 370.0
:1,3 : 370.0
:1,4 : 350.0
:2,0 : 360.0
:2,1 : 350.0
:2,2 : 370.0
:2,3 : 370.0
:2,4 : 350.0
:3,0 : 360.0
:3,1 : 350.0
:3,2 : 370.0
:3,3 : 370.0
:3,4 : 350.0
:4,0 : 360.0
:4,1 : 350.0
:4,2 : 370.0
:4,3 : 370.0
:4,4 : 350.0
:5,0 : 260.0
:5,1 : 250.0
:5,2 : 270.0
:5,3 : 270.0
:5,4 : 250.0
:6,0 : 260.0
:6,1 : 250.0
:6,2 : 270.0
:6,3 : 270.0
:6,4 : 250.0
:7,0 : 260.0
:7,1 : 250.0
:7,2 : 270.0
:7,3 : 270.0
:7,4 : 250.0
:8,0 : 260.0
:8,1 : 250.0
:8,2 : 270.0
:8,3 : 270.0
:8,4 : 250.0
:9,0 : 260.0
:9,1 : 250.0
:9,2 : 270.0
:9,3 : 270.0
:9,4 : 250.0
:10,0 : 210.0
:10,1 : 200.0
:10,2 : 220.0
:10,3 : 220.0
:10,4 : 200.0
:11,0 : 210.0
:11,1 : 200.0
:11,2 : 220.0
:11,3 : 220.0
:11,4 : 200.0
:12,0 : 210.0
:12,1 : 200.0
:12,2 : 220.0
:12,3 : 220.0
:12,4 : 200.0
:13,0 : 210.0
:13,1 : 200.0
:13,2 : 220.0
:13,3 : 220.0
:13,4 : 200.0
:14,0 : 210.0
:14,1 : 200.0
:14,2 : 220.0
:14,3 : 220.0
:14,4 : 200.0
:15,0 : 210.0
:15,1 : 200.0
:15,2 : 220.0
:15,3 : 220.0
:15,4 : 200.0
:16,0 : 210.0
:16,1 : 200.0
:16,2 : 220.0
:16,3 : 220.0
:16,4 : 200.0
:17,0 : 210.0
:17,1 : 200.0
:17,2 : 220.0
:17,3 : 220.0
:17,4 : 200.0
:18,0 : 210.0
:18,1 : 200.0
:18,2 : 220.0
:18,3 : 220.0
:18,4 : 200.0
:19,0 : 210.0
:19,1 : 200.0
:19,2 : 220.0
:19,3 : 220.0
:19,4 : 200.0
:20,0 : 110.0
:20,1 : 100.0
:20,2 : 120.0
:20,3 : 120.0
:20,4 : 100.0
:21,0 : 110.0
:21,1 : 100.0
:21,2 : 120.0
:21,3 : 120.0
:21,4 : 100.0
:22,0 : 110.0
:22,1 : 100.0
:22,2 : 120.0
:22,3 : 120.0
:22,4 : 100.0
:23,0 : 110.0
:23,1 : 100.0
:23,2 : 120.0
:23,3 : 120.0
:23,4 : 100.0
:24,0 : 110.0
:24,1 : 100.0
:24,2 : 120.0
:24,3 : 120.0
:24,4 : 100.0
:25,0 : 110.0
:25,1 : 100.0
:25,2 : 120.0
:25,3 : 120.0
:25,4 : 100.0
:26,0 : 110.0
:26,1 : 100.0
:26,2 : 120.0
:26,3 : 120.0
:26,4 : 100.0
:27,0 : 110.0
:27,1 : 100.0
:27,2 : 120.0
:27,3 : 120.0
:27,4 : 100.0
:28,0 : 110.0
:28,1 : 100.0
:28,2 : 120.0
:28,3 : 120.0
:28,4 : 100.0
:29,0 : 110.0
:29,1 : 100.0
:29,2 : 120.0
:29,3 : 120.0
:29,4 : 100.0

公共类InputMismatchException扩展了NoSuchElementException,由Scanner抛出,以指示检索到的令牌与预期类型的​​模式不匹配,或者令牌超出预期类型的​​范围。

priceArray [i] [j] = Double.parseDouble(scan.next());

您可以通过添加更多有用的错误输出来帮助您寻找缺陷。

private void start() {
    for (int i = 0; i < MAX_ROW; i++) {
        for (int j = 0; j <  MAX_COLUMN; j++) {
            try {
                priceArray[i][j] = scan.nextDouble();
            } catch (Exception e) {
                System.err.println("Error discovered at " + i + "," + j ": " + e);
            }
        }
    }
}

这至少会告诉您输入的行/列不正确。

您会看到如下错误:

Error discovered at 0,5: java.util.InputMismatchException

应该向您表明扫描仪超出了行尾。 您需要保护对nextDouble()调用:

                if (scan.hasNextDouble()) {
                    priceArray[i][j] = scan.nextDouble();
                    System.out.println(":" + i + "," + j + " : " + priceArray[i][j]);
                }

您还需要前进到每一行的下一行:

        if (scan.hasNextLine()) {
            scan.nextLine();
        }

以下修改后的start()函数应正确解析您的输入:

// fills the priceArray with numbers from seatPrices.txt
private void start() {
    for (int i = 0; i < MAX_ROW; i++) {
        for (int j = 0; j <  MAX_COLUMN; j++) {
            try {
                if (scan.hasNextDouble()) {
                    priceArray[i][j] = scan.nextDouble();
                    System.out.println(":" + i + "," + j + " : " + priceArray[i][j]);
                }
            } catch (Exception e) {
                System.err.println("Error discovered at " + i + "," + j + ": " + e);
            }
        }
        if (scan.hasNextLine()) {
            scan.nextLine();
        }
    }
}

如果您在Java文档中检查Scanner类,它会说,

如果下一个标记不能转换为有效的double值,则InputMismatchException nextDouble()方法将引发InputMismatchException 如果翻译成功,则扫描程序将前进经过匹配的输入。

尽管在代码中将定界符设置为: ,但遇到新行.ie \\n时,代码将中断。 因此,要使您的代码将数据存储在double类型的2D矩阵中,请尝试将扫描仪更改为:

Scanner sc = new Scanner(new FileInputStream(new File(pathOfInpFile));

并将start()方法中的for循环更改for

for(int row = 0; row < rowcount; row++){
                //Read each line. No. of lines will be equal to the no. of rows in file.
                String eachLine = sc.nextLine();
                /*
                 * Split each line at ':'. This gives you a String array of 
                 * length equal to no. of columns in file.
                 */
                String[] columnData = eachLine.split(":");
                for(int col = 0; col < colCount; col++){
                    //Now, store data in your matrix.
                    data[row][col] = Double.parseDouble(columnData[col]);
                }
            }

我希望这能解决您的问题。

暂无
暂无

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

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