The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Re: Java error discovered?

The program takes a list of names and puts them in alphabetical order. I can tell you for sure, that everything works except for the printing to a file. Those commented out System.out.prints show that the list is fully alphabetized. When the file is printed out - all it shows is one name of the 397.

I just tried it with a new list of 10 names - for some reason, the alphabetizing didn't work, nevertheless, the problem is the same - I can't write to file. Just copy all that up there to a file called alphabetizer.java (I can't attach files with this), and then I put this random list of names to a file, list_of_names.txt :

Re: Java error discovered?

It's a common reaction to "blame Java" when something unfamiliar happens. But think about it this way: which is more likely: you discovering a flaw that nobody else has found in a common API used by millions of people? Or that you just don't fully understand what the code is doing?

I'm not trying to come off as judgmental, but it's a good practice to always blame yourself first: what am I misunderstanding here? What did I miss?

I'd recommend you step through this with a debugger, or at least add some print statements, to figure out what's going on. Read through the APIs of everything you're using to make sure you aren't incorrectly assuming something. If you still can't figure it out, I suggest putting together an SSCCE that demonstrates exactly what you're talking about in as few lines as possible (which, in your case, should only be a few).

Re: Java error discovered?

Try abstracting your code. Break it up into smaller methods which each perform one task (for example, have a method which reads the file in, another one which takes a list of names and sorts them, and a third one which writes the file out). This will help you to analyze each section independent of the others to determine if they work as expected. It will also make your code much easier to read and decipher.

There are a few things you can get rid of. For example, this:

listlength = count;

Arrays already have a length field (this isn't C or C++ where you need to track length separately). Use that instead. You don't need to manually set it or anything, it's automatically set when you create the array.

As far as writing to a file goes, try using a PrintStream. This is the same output mechanism used by System.out and System.err and is my personal favorite for generic file output.

Re: Java error discovered?

Yes, a genuine Java error discovered. Printing to file doesn't work if you use a for loop to do it - the problem is the Try statement. It only allows runs once for instance - the last one. Thus - I've got to write the file all in one shot - if that's even possible.

Re: Java error discovered?

Originally Posted by Programming_Hobbyist

Yes, a genuine Java error discovered. Printing to file doesn't work if you use a for loop to do it - the problem is the Try statement. It only allows runs once for instance - the last one. Thus - I've got to write the file all in one shot - if that's even possible.

I'll contact Sun and Oracle.

Are you being sarcastic? I promise that this is not an actual bug- you're just misunderstanding something. Please post an SSCCE that demonstrates exactly what you're talking about in as few lines as possible.

There are channels to report bugs, and they'll need an SSCCE as well, so you might as well let us check it over for you.

Re: Java error discovered?

Yes - there's no easy way to write many lines to a file using a loop in Java. There might be a way, but I couldn't find it - perhaps bytebuffer can do it. I switched over to C++ - instantaneous success. An illustration as to why C++ is better.

Re: Java error discovered?

Try saving the following to testing.java. You'll see it writes to the file - but it only writes one line instead of 10. It does that with the three different methods of writing to a file that I've found. I'll send this error in Java's code to Sun/Oracle.

Writes bytes to a file. The options parameter specifies how the the file is created or opened. If no options are present then this method works as if the CREATE, TRUNCATE_EXISTING, and WRITE options are present. In other words, it opens the file for writing, creating the file if it doesn't exist, or initially truncating an existing regular-file to a size of 0. All bytes in the byte array are written to the file. The method ensures that the file is closed when all bytes have been written (or an I/O error or other runtime exception is thrown). If an I/O error occurs then it may do so after the file has created or truncated, or after some bytes have been written to the file.

Your file code isn't appending because you never specified that it should append. Rather, you're continually opening up the file, truncating the file to 0 size, writing to it, closing the file, then repeating the process.

Re: Java error discovered?

There's was a ghost in my machine. I tried that printstream before, and several others, and none worked. Would simply not write anything to the file. I've met this phenomenon before - with Microsoft Visual Studio 2010. It was giving me an error - couldn't find the include file "stdafx.h" when trying to create a Windows Form application file - then it just vanished somehow, after I found a way around it (first compiling with just that include file...then compiling with the other include files).

But now I've encountered success. Both solutions work, but then that doesn't explain those errors Java was giving me. For the Files.write, when there's no existing file - it should still work but it doesn't. When setting it to append - then it works for some reason. Without the append option, it creates one; it doesn't need to truncate it to 0 if it doesn't exist or open an initial. Thus - there's an error with append, and not the Try/Catch. This same problems exists for:

I tried printstream just now, by putting it into a different method, and calling that method from the main method, with a try/catch to catch the IOException error. That didn't work - it only printed the last line. However, I tried it in the public static main, with a throws right next to "public static void main", and without the try/catch, and then it worked. But then I switched back to the first way, putting it in a different method, calling it from the main method with a try/catch statement, and now that one worked. There's a ghost in my machine.

So problem solved. This was highly unusual behavior from my computer - not really human error.

Re: Java error discovered?

It's entirely possible that with my latest attempt at making the printstream with my main program - that I might've compiled a different, earlier version of these programs (possibly the brief example that I posted here with append), and when I tried that method again after getting it to work in the main method, then it worked correctly. Thus explaining that paradox. However - we have discovered an error with those write methods. As for my first attempts yesterday at printing without other methods - I must've tried some outdated/deprecated methods, and not that one.

Re: Java error discovered?

I stand by my statement that this is almost definitely not a "Java error". You've listed several sources of potential human error, and nobody (including you) is able to reproduce your original problem. This is exactly why we ask for an SSCCE- it forces you to check all of your assumptions, including about which version of your own code you're running.

I would guess that the ghost in your machine is in fact a result of some misunderstanding about what the code was doing, and then a misunderstanding about which code you were actually running. Again, not a Java error. This is why I suggested that you shouldn't jump to that conclusion before checking all of your assumptions.

Re: Java error discovered?

All HelloWorld did was point out your misunderstanding of Java, not that there is an intrinsic error within Java that you've magically just discovered. But go on and continue living in whatever fantasy world makes you happy.

Re: Java error discovered?

Well thanks everyone for the help. Why do you people even post on this forum? Is it the social aspect? It must be the social aspect. Anyway - this is part of my larger project. Now that I can write to file, I can save data to a file, and then retrieve that data. I'm building a project where I can add people, their attributes, and then easily sort them according to attributes, for instance weight, height, eye-color, etc, and then show images of these people according to the sorting criteria.

Re: Java error discovered?

Helloworld992 helped me figure it out - there's something wrong with .write. There's something call the scientific method. If something is obviously wrong - you tend to accept that as fact.

Hahahaha, no, you tend to try to repeat the experiment! If you (and others) can't repeat the results, then something must have been wrong with your original findings. In this case, you made an error in the code or in your assumptions. And that's fine, it happens to every single one of us, which is why it's not very wise to conclude that you found a major bug in Java every time.