0

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();
}
Andi Emma Davies
  • 298
  • 4
  • 19
Raziel
  • 1,448
  • 2
  • 17
  • 35
  • So you want to `do { /* everything there */ } while(isDone.equals("N");` ? – Peter Lawrey Oct 07 '15 at 09:50
  • Possible duplicate of [Resetting a .nextLine() Scanner](http://stackoverflow.com/questions/13991494/resetting-a-nextline-scanner) – Deltharis Oct 07 '15 at 09:52
  • 1
    I suggest you to use boolean true/false as `isDone` values instead of checking strings Y/N, it'll be simplier and more correct. – solar Oct 07 '15 at 09:53
  • The way I understand it, the scanner continues reading the file instead of starting from the beggining - which is not suprising, since it's the same scanner with no code trying to do anything about the issue - and for what to do about it see my duplicate flag – Deltharis Oct 07 '15 at 09:53

1 Answers1

3

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??

Matthias J. Sax
  • 59,682
  • 7
  • 117
  • 137
  • This is what i was looking for. had no reason not to use boolean, but considering what its doing, i will switch that to a boolean. – Raziel Oct 07 '15 at 10:00