1

So, I'm still new to programming and this site. I've been trying to make my own browser just for fun and I'm learning how to write and read from text files to use as logs to save data. I'm trying to make a Favorites system so that I can save different websites to a log but I can't understand why part of my code isn't working. I've spent about two and a half hours on the web looking around and I can't see any issues.

try(BufferedReader format1 = new BufferedReader(new 
                                         FileReader("favorites.txt"))) {

            String line = format1.readLine(); //Used to look through file
            String found = ""; //Stores the address that was the same

            //Loop searches file, I only plan to have a max of 20 favorites
            for (int counter = 0; counter < 20; counter++) {

                System.out.println(line); //Just used so I can make sure the 
                                            loop works

                if(line.equals(input)) {
                    counter = 20;
                    found = line;
                }

                line = format1.readLine();
            }
            format1.close();

            if(!(found.equals(input))) {
                BufferedWriter format2 = new BufferedWriter(new 
                                         FileWriter("favorites.txt", true));
                format2.write(input);
                format2.newLine();

                format2.close();


            }           
    } catch (IOException e1) {
        System.out.println("ERROR! Favorite not Added.");
    }

I've checked to see where the problem lies and the if statement that actually writes to the file seems to be where the problem is. I've looked a million times and I can't see anything. Maybe a more experienced programmer could see my issue? Whenever I run this section of code, it spits out a ton of error messages. I appreciate any help!

  • "It spits out a ton of error messages". What did the error messages say? You're supposed to read those to [understand what the problem is](https://stackoverflow.com/q/3988788/2775450). If, after reading that post and examining your stack trace you still can't figure out the problem, post the complete stack trace in the question and show which line in the code is throwing the exception. – James_D May 24 '17 at 20:39
  • @James_D I tried to but, since I'm relatively new, I wasn't too sure what they meant. A lot had to do with exceptions in the Application. The weird part is when I comment out the for loop, it works fine. Not sure what is going on. – Mega Trainer May 24 '17 at 20:54
  • That's why I linked the post that explains how to read the stack trace. – James_D May 24 '17 at 20:55

1 Answers1

2

I suspect your issue stems from this bizarre "max 20 favourites" loop, which will try to read 20 lines whether the file contains them or not (the end result being that it throws a NullPointerException with less than 20 lines, and ignore any more than 20 lines). It seems like you've tried to simplify things by only reading a certain number of lines so you don't have to work out how many to read, but in the process of doing so have actually made things more complicated!

There's a reasonably common idiom for reading an entire file line by line in Java - try replacing your for loop (the one with counter as the iterator) with this:

String line;
while((line=format1.readLine())!=null) {
    System.out.println(line); //Just used so I can make sure the 
    if (line.equals(input)) {
        found = line;
        break;
    }
}
format1.close();

This will read each line of the file into line while the returned value from readLine() isn't null, which it will be when it hits the end of the file.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • 1
    That fixed it! Thanks so much. You're right that I tried to simplify it. I see now that that was a bad move. I'll know what to do for next time. – Mega Trainer May 25 '17 at 01:40