简体   繁体   中英

Reading a file from Scanner but it won't print

I have a class that reads a section of a txt file. The code works for some people but to me it won't print to console or attach to my JTextArea(my ultimate goal)

I just want to find out if its the code or the file that is the issue.

 public void readFiles(String fileString)
        throws FileNotFoundException {

    file = new File(fileString);
    Scanner scanner = null;
    String line = "";

    // access file
    try {
        scanner = new Scanner(file);
    } catch (FileNotFoundException e) {
        System.out.println("File not found.");
        return; // don't continue if the file is not found
    }

    // if more lines in file, go to next line
    Boolean started = false;
    while (scanner.hasNext()) {

        line = scanner.nextLine();
        if (line.equals("BGEND")) {
            started = false;

        }

        if (started) // tag in the txt to locate position
        {

            System.out.println(line);//won't print on my console
            lb1.setText(line); //attaches to a JTextArea.
            window2.add(lb1); //adds to JPanel
        }

        if (line.equals("BGSTART")) {
            started = true;
        }
    }
    scanner.close();
} 

This is my file

BGSTART   
Ashley the principal at Leicester,
Memorial School has been given the
task of matching some students names
to their bus numbers and departure
time, after their computer system went 
down. Using only the clues that follow, 
match each student to their bus number 
and route to determine who goes where 
and when!
Remember, as with all grid-based logic 
puzzles, no option in any category will 
ever be used more than once. If you get 
stuck or run into a problem try the Clear 
button to remove any mistakes that might 
be present on the grid, or use one of your 
4 hints with the Hint button to see what is 
the next logical step to solve the puzzle.
BGEND
        while (scanner.hasNext()) {
        line = scanner.nextLine();
        if (line.equals("BGSTART")) {
            started = true;
        }

        if (line.equals("BGEND")) {
            started = false;

        }
        if (started) // tag in the txt to locate position
        {

            System.out.println(line);//won't print on my console
            lb1.setText(line); //attaches to a JTextArea.
            window2.add(lb1); //adds to JPanel
        }

    }

This way the program checks first if the first line equals to the "BGSTART" string, sets the boolean to true and will continue to the next if statement and execute that code as long as it reaches the end of the file.

Boolean started = false;
while (scanner.hasNext()) {

    line = scanner.nextLine();
    if (line.startsWith("BGSTART")) {
        started = true;
        continue;
    }

    if (line.startsWith("BGEND")) {
        started = false;
        break;
    }

    if (started) // tag in the txt to locate position
    {
        System.out.println(line);//won't print on my console
        lb1.setText(line); //attaches to a JTextArea.
        window2.add(lb1); //adds to JPanel
    }
}
scanner.close();

Try this. Basically, if you have something like:

some lines of text here
BGSTART
some lines of text here
BGEND

The code should skip the lines of text preceding the BGSTART marking. Once this marker is encountered, the boolean flag value changes to TRUE to let the code know you started reading valid lines, and immediately skip the rest of the code to get the next line. If the BGEND marker is encountered, it signifies the end. So in this case, you want to break completely out of the loop. Then, if the boolean flag is set to TRUE, you want to do something with the line of text read from the file.

I think the above code does exactly what you need. It should also work for a case like this:

some lines of text here
BGSTART
some lines of text here
BGEND
some lines of text here
BGSTART
some lines of text here
BGEND

Effectively reading only those lines of text between BGSTART and BGEND, and not those between BGEND and BGSTART.

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