簡體   English   中英

使用不同長度的行讀取 Java 中的文件時出現問題

[英]Problem reading file in Java with lines of different length

我正在嘗試讀取文件,但無法從中獲得正確的輸出。 有人可以告訴我應該如何更改代碼以使其正常工作嗎? 代碼中的 isNum() 函數是一種檢查字符串是否為數字的方法(因為我需要將 5 和 10 放在一個單獨的變量中)。

編輯:在聽取了建議后,我對代碼進行了一些更改,現在看起來好多了,但仍然存在一些問題。 下面的代碼和輸出已更新。

        int numEv = 0;
        Scanner input = new Scanner(System.in);
        ArrayList<String> evtList = new ArrayList<String>();
        try {
            input = new Scanner(Paths.get("src/idse/Events.txt"));
        } catch (IOException e) {
            System.out.println(e);
        }

        try {
            
            while(input.hasNext()) {
                String a = input.nextLine();
                
                if (isNum(a)){
                    numEv = Integer.parseInt(a);
                    System.out.println(numEv);
                }
                else if(!a.isEmpty()&&!isNum(a)){
                    String[] parts = a.split(":");
                    for (String part : parts) {
                        evtList.add(part);
                    }
                    System.out.println(evtList);
                }
                if(isNum(a)){
                    evtList.clear();
                }
                
            }

我得到的輸出是:

5
[Logins, 2, Total time online, 1, Emails sent, 1, Orders processed, 1]
[Logins, 2, Total time online, 1, Emails sent, 1, Orders processed, 1, Pizza’s ordered online, 0.5]
10
[Logins, 7, Total time online, 5, Emails sent, 9, Orders processed, 15]
[Logins, 7, Total time online, 5, Emails sent, 9, Orders processed, 15, Pizza’s ordered online, 0.9, Logouts, 6]

我想要的輸出是:

5
[Logins, 2, Total time online, 1, Emails sent, 1, Orders processed, 1, Pizza’s ordered online, 0.5]
10
[Logins, 7, Total time online, 5, Emails sent, 9, Orders processed, 15, Pizza’s ordered online, 0.9, Logouts, 6]

您應該進行 3 次修復,請按照以下步驟操作:

  1. 更正您的文件格式。 將格式更改為:
5
Logins:2:Total time online:1:Emails sent:1:Orders processed:1:Pizza’s ordered online:0.5:
10
Logins:7:Total time online:5:Emails sent:9:Orders processed:15:Pizza’s ordered online:0.9:Logouts:6:

Thud 將根據需要按行分割文件。

  1. 在代碼塊中輸入System.out.println()方法:
if (isNum(a)){
      numEv = Integer.parseInt(a);
      System.out.println(numEv);
}
else if(!a.isEmpty()&&!isNum(a)){
     String[] parts = a.split(":");
     for (String part : parts) {
          evtList.add(part);
     }
     System.out.println(evtList);
}

這將修復你太長的輸出,因為它打印了一些不必要的東西。

  1. 清除事件列表:
evtList.clear();

在 while 循環中的每次迭代后添加此行,以使列表更新到當前行,而不是之前事件中的所有節點。

根據您在注釋中指定的內容(例如,您不能更改輸入文件格式),您必須始終檢查文件的下一行以查看特定輸入代碼是否已結束。 我會使用這個技巧在不移動指針的情況下讀取下一行。

int numEv = 0;
Scanner input = new Scanner(System.in); // idk what you need this for
ArrayList<String> evtList = new ArrayList<String>();
try {
    BufferedReader reader = new BufferedReader(new FileReader(Paths.get("src/idse/Events.txt")));
} catch (IOException e) {
    System.out.println(e);
}

try {
        
    while((a= reader.readLine()) != null) {
        if (isNum(a)){ // Reading and printing the number
            numEv = Integer.parseInt(a);
            System.out.println(numEv);
        } else if(!a.isEmpty()){ // Getting and storing the code
            String[] parts = a.split(":");
            for (String part : parts) {
                evtList.add(part);
            }
        }
        reader.mark(0);
        a = reader.readLine();
        if(a == null || isNum(a)) { // If the next line is a number or doesn't exist, we print and clear the code
            System.out.println(evtList);
            evtList.clear();
        }
        reader.reset();
    }

我希望這有效!

暫無
暫無

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

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