简体   繁体   中英

Resetting Java while loop

I currently have 2 loops, one which gets a timestamp, and another while loop to find the mapped information based off that time stamp and output in a certain way.

Issue I have is I am currently looping through a text, and want it to start reading the file from the beginning again when the isdone="N" for the second loop, however, this does not seem to be the case.

Code so far:

public static void organiseFile() throws FileNotFoundException {
    String            directory = "C:\\Users\\xxx\\Desktop\\Files\\ex1";
    Scanner           fileIn    = new Scanner(new File(directory + "_temp.txt"));
    Scanner           readIn    = new Scanner(new File(directory + ".txt"));
    PrintWriter       out       = new PrintWriter(directory + "_ordered.txt");
    ArrayList<String> lines     = new ArrayList<String>();

    String readTimeStamp     = "";
    String timeStampMapping  = "";
    String outputFirst       = "";
    String outputSecond      = "";
    String outputThird       = "";
    String previousTimeStamp = "";
    String doneList          = "";
    String isdone            = "";

    int counter = 1;

    // Loop to get time stamps
    while(fileIn.hasNextLine()) {
        readTimeStamp = fileIn.nextLine();

        if(readTimeStamp != null && readTimeStamp.trim().length() > 0) {
            readTimeStamp = readTimeStamp.substring(12, 25);
            System.out.println(readTimeStamp);

            // Previous time stamp found, no need to loop through it again
            if(doneList.contains(readTimeStamp))
                isdone = "Y";

            // Counter in place to stop outputting the first record, otherwise output file and clear variables down
            else if(!previousTimeStamp.equals(readTimeStamp) && counter > 1) {
                out.println(outputFirst + outputSecond + outputThird);  
                System.out.println("Outputting....");
                outputFirst  = "";
                outputSecond = "";
                outputThird  = "";
                counter      = 1;
            }

            // New time stamp found, start finding values in second loop
            else
                isdone = "N";

            // Secondary loop to find match of record
            while(readIn.hasNextLine() && isdone.equals("N")) {
                System.out.println("Mapping...");
                timeStampMapping = readIn.nextLine();
                System.out.println(timeStampMapping);

                // When a record has been found with matching time stamps, start ordering
                if(timeStampMapping.contains(readTimeStamp)) {
                    previousTimeStamp = readTimeStamp;
                    System.out.println(previousTimeStamp);

                    if(timeStampMapping.contains("[EVENT=agentStateEvent]")) {
                        outputFirst += timeStampMapping + "\r\n";
                    } else if(timeStampMapping.contains("[EVENT=TerminalConnectionCreated]")) {
                        outputSecond += timeStampMapping + "\r\n";
                    } else {
                        outputThird += timeStampMapping + "\r\n";
                        doneList    += readTimeStamp + ",";
                    }

                    counter++;
                }
            }
        }
    }

    System.out.println("Outputting final record");
    out.println(outputFirst + outputSecond + outputThird);
    System.out.println("Complete!");
    out.close();
}

You can use Scanner.reset() to reset it to the beginning of the file. For example, after your second while -loop include:

if (isdone.equals("Y")) {
    fileIn.reset();
}

Btw: why are you using String for isdone instead of boolean ??

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