user3739406
user3739406

Reputation: 364

Simple textfile not being parsed correctly

I have a simple textfile as such:

type = "Movie"
year = 2014
Producer = "John"
title = "The Movie"

type = "Magazine"
year = 2013
Writer = "Alfred"
title = "The Magazine"

// And I'm trying to parse at the moment, the word "Movie" and "Magazine", what's weird is, my program seems to always skip and go to "Magazine", yet there is nothing wrong with my textfile.

public void Parse() throws IOException {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(new File("test.txt")));

            String line;
            boolean flag;
            flag = false;

            while ((line = reader.readLine()) != null) {

            if (line.contains("=")) {
                String[] bits = line.split("=");
                String name = bits[0].trim();
                String value = bits[1].trim();

                if (name.equals("type")) {
                    if(value.equals("\"Movie\"")) 
                        flag = false;
                    if(value.equals("\"Magazine\"")) 
                        flag = true;
                }
             }
             if(flag == true)
                 System.out.println("flag is true");
             if(flag == false)
                 System.out.println("Flag is false");
            }
        } catch (FileNotFoundException e) {

        }

After checking a few if statements after the code i.e if flag == true or false, it always returns true, not false. So for some strange reason, I'm never able to parse the word "Movie", this line : if(value.equals("\"Movie\"")) never returns true, I'm not sure what's wrong here, any ideas would be much appreciated.

Upvotes: 0

Views: 36

Answers (1)

Spencer
Spencer

Reputation: 108

One issue I see is that "flag" is declared outside the while loop. Another is that return type is void, so I don't see how the method is returning true or false.

You are iterating through the entire file, which contains movie and magazine. The final state of flag will always be true.

If you place movie at the end of the file, is flag set to false?

Upvotes: 2

Related Questions