views:

50

answers:

2

Hi guys.

I have created a program which takes a text file full of 3 letter words and processes them, stores them in an array and then outputs to the build output in JCreator and then writes the same output to a file.

Now, this program works fine, but when I print a lot of data - I get all of these blank lines inserted where there shouldn't be any.

I use this to print to my file:

PrintWriter fw = new PrintWriter(new FileWriter("Dictionary.txt")); 

   for (int i=0; i<count; i++)
   {
       if (words[i]!=null)
           fw.println(words[i]);
   }

I loop through the array and don't print to a file, just to my output screen on the IDE. Now, I will suddenly get a blank line where there shouldn't be, like so:

tut
tux

uke
use

and it seems to be completely random.

Now how would I remove these lines from the file without having to write to a new file, as writing large amounts of lines seems to cause this problem.

Thanks guys

+3  A: 

Make a small change:

if (words[i]!=null && words[i].trim().length() != 0)
       fw.println(words[i]);

Basically, you want to check and see if the line would be blank, and skip printing it.

jjnguy
Removing the empty lines from the dictionary seems like a good move indeed :)
extraneon
@extra, Yup, probably...
jjnguy
I thought so too, but it's random so it doesn't seem to be that.
Colin Hebert
@Colin, my guess is that it isn't actually random.
jjnguy
Hi Justin, it worked. The blank lines have been removed. Thank you. BTW, it's .length(), my IDE spat that at me...
Strategist01
@Strate, glad I could help. Thanks for pointing out my typo!
jjnguy
@Justin: No worries, this has helped a lot :)
Strategist01
A: 

If it's totally random, then you should try to write in a file and check this file. Not that I don't trust the console provided by your IDE.

Colin Hebert
I wrote to the file, and the output to the console and the file are exactly the same.
Strategist01