简体   繁体   中英

Problem reading file in Java with lines of different length

I am trying to read a file but I am not able to get the correct output from it. Can someone tell me how should I change the code to make it work? isNum() function in the code is a method that checks whether the string is a number or not (because I need to put 5 and 10 in a separate variable).

Edit: I have changed the code a bit after listening to the suggestions and it looks better now but there still some problem. The code and output below has been updated.

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

The output that I am getting is:

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]

The output that I want is:

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]

There are 3 fixes you should do, follow the next steps:

  1. Correct your file format. change the format to:
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 will sperate the file by lines as you want.

  1. Enter the System.out.println() method to the code blocks:
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);
}

This will fix you too long output, because its prints some unneccery stuff.

  1. Clear the event list:
evtList.clear();

Add this line after every iteration in the while loop, to make list update to the current line, and not full of nodes from previous events.更新到当前行,而不是之前事件中的所有节点。

Based on what you specified in the comments (eg You cannot change the input file format), you would always have to check the next line of the file to see if the specific input code has ended. I would use this trick to read the next line without moving the pointer.

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

I hope this works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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