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

时间:2016-11-21 07:10:40

标签: java arrays delimiter filereader

我的这个程序的目标是从一个名为seatPrices.txt的输入文本文件中获取数字,并将它们保存在一个名为priceArray的double类型数组中。

输入文件是:

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

3 个答案:

答案 0 :(得分:1)

public class InputMismatchException 扩展NoSuchElementException 由扫描程序抛出,表示检索到的令牌与预期类型的​​模式不匹配,或者令牌超出预期类型的​​范围。

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

答案 1 :(得分:1)

您可以通过添加更多信息性错误输出来帮助您寻找缺陷,例如

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();
        }
    }
}

答案 2 :(得分:1)

如果您查看Scanner类的java文档,则说明了

  

方法nextDouble()会抛出InputMismatchException   下一个令牌无法转换为有效的double值。如果   翻译成功,扫描仪超过了输入   匹配。

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

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

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

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]);
                }
            }

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