0

I have a textfile as such:

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

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

What I'm trying to do is, first, search the file for the type, in this case "Movie" or "Magazine".

If it's a Movie, store all the values below it, i.e Set the movie variable to be 2014, Producer to be "John" etc.

If it's a Magazine type, store all the variables below it as well separately.

What I have so far is this:

public static void Parse(String inPath) {
        String value;
        try { 
            Scanner sc = new Scanner(new FileInputStream("resources/input.txt"));

            while(sc.hasNextLine()) {
                String line = sc.nextLine();
                if(line.startsWith("type")) {
                    value = line.substring(8-line.length()-1);
                    System.out.println(value);

                }

            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(LibrarySearch.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

However, I'm already having an issue in simply printing out the first type, which is "Movie". My program seems to skip that one, and print out "Magazine" instead.

For this problem solely, is it because the line: line.startsWith("type")is checking if the current line in the file starts with type, but since the actual String called lineis set to the nextline, it skips the first "type"?

Also, what would be the best approach to parsing the actual values (right side of equal sign) below the type "Movie" and "Magazine" respectively?

user3739406
  • 254
  • 1
  • 3
  • 16

1 Answers1

2

I recommend you try the following:

BufferedReader reader = new BufferedReader(new FileReader(new File("resources/input.txt")));

String line;

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")) {
            // Make a new object
        } else if (name.equals("year")) {
            // Store in the current object
        }
    } else {
        // It's a new line, so you should make a new object to store stuff in.
    }
}

In your code, the substring looks suspect to me. If you do a split based on the equals sign, then that should be much more resilient.

Matt Eskridge
  • 1,019
  • 10
  • 24
  • Just wondering how I would differentiate between type Movie and Magazine here with your code? – user3739406 Nov 12 '15 at 00:57
  • After the check to see if name is equal to type, add a check inside that block to see if value is equal to Movie. Example: if (value.equals("Movie")) { } – Matt Eskridge Nov 12 '15 at 01:00
  • So I just noticed but, using this always seems to skip the type "Movie" and goes to Magazine instead (Just as my original program). Any ideas? – user3739406 Nov 12 '15 at 02:36
  • That's odd. I recommend checking to make sure your data file is correct, then also adding this statement at the beginning of your while loop to make sure it's actually reading the lines: System.out.println(line); – Matt Eskridge Nov 12 '15 at 02:53
  • Hmm, the printout certainly prints the whole file. I'm doing something along the lines of: if(value.equals("\"Magazine\"")) flag = true;if(value.equals("\"Movie\"")) flag = false; And then using a bunch of if statements to add add to variables if flag is true or false, except the ones where flag is false (Movie) never run. – user3739406 Nov 12 '15 at 03:12
  • Interesting. Have you tried a data set which has a movie, then a magazine, then another movie? Is it ignoring all movies, or just the first entry? It may be useful to update the original question with the current source code (or at least relevant portions). May also be worth changing the flag to an integer where 0 is neither, 1 is Movie, and 2 is Magazine. That may help resolve issues that may arise the first go around when the flag is set to be a movie even though a movie hasn't been encountered yet – Matt Eskridge Nov 12 '15 at 07:24