用Java读取不同长度的行时出现问题

问题描述

我正在尝试读取文件,但是无法从文件获取正确的输出。有人可以告诉我如何更改代码以使其正常工作吗?代码中的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,Orders processed,1]
[Logins,Pizza’s ordered online,0.5]
10
[Logins,7,5,9,15]
[Logins,15,0.9,logouts,6]

我想要的输出是:

5
[Logins,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循环中的每次迭代之后添加此行,以使列表仅仅更新到当前行,而不会充满以前事件中的节点。

,

根据您在注释中指定的内容(例如,您无法更改输入文件格式),您将始终必须检查文件的下一行以查看特定输入代码是否已结束。我将使用this trick来读取下一行而不移动指针。

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

我希望这行得通!