簡體   English   中英

從輸入文件讀取到整數數組

[英]Reading from an Input File to an Integer Array

我目前正在用Java編寫一個項目,該項目將使用一個輸入文件並將其讀取到幾個並行數組中。 有幾個限制-我們不能使用數組列表,必須使用Scanner讀取文件。 將其讀入數組后,還需要執行一些其他步驟,但是遇到了麻煩。

    public static void main(String[] args) throws FileNotFoundException {

    final int ARRAY_SIZE = 10;
    int choice;
    int i, variableNumber;
    String[] customerName = new String[ARRAY_SIZE];
    int[] customerID = new int[ARRAY_SIZE];
    String[] os = new String[ARRAY_SIZE];
    String[] typeOfProblem = new String[ARRAY_SIZE];
    int[] turnAroundTime = new int[ARRAY_SIZE];

    readFile(customerName, customerID, os, typeOfProblem, turnAroundTime);

}

public static void readFile(String[] customerName, int[] customerID, String[] os, String[] typeOfProblem, int[] turnAroundTime) throws FileNotFoundException
{
    File hotlist = new File("hotlist.txt");
    int i = 0;

    if (!hotlist.exists())
    {
        System.out.println("The input file was not found.");
        System.exit(0);
    }
    Scanner inputFile = new Scanner(hotlist);
    while (inputFile.hasNext())
    {
        customerName[i] = inputFile.nextLine();
        System.out.println(customerName[i]);
        customerID[i] = inputFile.nextInt();
        os[i] = inputFile.nextLine();
        typeOfProblem[i] = inputFile.nextLine();
        turnAroundTime[i] = inputFile.nextInt();
        i++;
    }
    System.out.println("This is only a test." + customerName[1] + "\n" + customerID[1] + "\n"
                        + os[1] + "\n" + typeOfProblem[1] + "\n" + turnAroundTime[1]);
}

當我嘗試運行上述代碼時,它失敗並顯示以下錯誤:

run:
Mike Rowe
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.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at mckelvey_project3.McKelvey_Project3.readFile(McKelvey_Project3.java:70)
    at mckelvey_project3.McKelvey_Project3.main(McKelvey_Project3.java:33)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

hotlist.txt文件的內容如下:

Mike Rowe
1
Windows DOS
Too Much ASCII Porn
3
Some Guy
2
Windows 10
Too Much Windows
200

任何幫助是極大的贊賞! 順便說一下,當我嘗試調試代碼時,所有System.out語句都是測試語句。 我已經將錯誤隔離為

customerID[i] = inputFile.nextInt();

同樣

turnAroundTime[i] = inputFile.nextInt();

但無法弄清楚為什么這些語句不起作用。

當您調用Scanner.nextInt()它僅使用int ,並且在此留下任何結尾的空格或換行符。 相反,您可以使用類似的方法,

Scanner inputFile = new Scanner(hotlist);
while (inputFile.hasNext()) {
    customerName[i] = inputFile.nextLine();
    System.out.println(customerName[i]);
    String custId = inputFile.nextLine();
    customerID[i] = Integer.parseInt(custId);
    os[i] = inputFile.nextLine();
    typeOfProblem[i] = inputFile.nextLine();
    String turnAround = inputFile.nextLine();
    turnAroundTime[i] = Integer.parseInt(turnAround);
    i++;
}

我得到(連同您的代碼/文件),

Mike Rowe
Some Guy
This is only a test.Some Guy
2
Windows 10
Too Much Windows
200

您的主要問題是您沒有設置適當的分隔符。 初始化掃描程序后,請執行inputFile.useDelimiter("\\n")將定界符設置為換行符-默認為空格。

然后,您可以讀取一個String inputFile.next()並使用一個int inputFile.nextInt()沒有任何問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM