If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

For some reason I'm getting an ArrayIndexOutOfBoundsException. I have a file called numbers.dat with a 13 numbers in it, and I'm trying to read from it, preform operations on the numbers, and output the results to both the JFrame and a new file. Can someone shed some light on what I'm doing wrong?

I figured out how to get rid of the ArrayIndexOutOfBounds error, but now another error showed up. Here is the error message:

Code:

Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at FileStats.doStats(FileStats.java:89)
at FileStats.readFile(FileStats.java:54)
at FileStats.main(FileStats.java:122)

First, you're looking for 15 numbers when you set up your loop like this:

Code:

int count = 0;
while (count != 14)
{
...
count++;
}

Second, in your readFile method, you have already done 2 readLine() calls, which causes you to skip on of the numbers altogether, and leaves you with only 11 numbers to parse.

So take out at least one of the readLine calls before the loop in readFile, and make sure you're only looking for 12 numbers in doStats. Something like this:

Code:

int count = 0;
while(count<12)
{
...
count++;
}

As for this while loop, I'd suggest using a for loop. But the results are the same, so using while isn't causing you any problems, per se.

I'd also suggest you move the BufferedReader and PrintWriter declarations outside of the try{} block, and put the calls to their close() methods a finally{} block. It's still not ideal, but it's much better. And at least you'll be able to see some of your output in the output file even when you get exceptions.

Is there a way specifically to check if the file has reached its end (no more numbers)? That way one could add as many numbers as they wanted and it would still work.

Also, the program seems to be counting the numbers in the file from the bottom up. The top two numbers are not being counted into the total sum, and if they are removed the program ceases functioning. So the above method of checking for the end of the file would be very helpful.

Thank you for the help so far, at least the program is running now =p.

when readLine() returns a null, as opposed to a "", it has reached the end of what is available. In stead of a counter in your while loop, you could put the readLine() in the while statement in doStats.

when readLine() returns a null, as opposed to a "", it has reached the end of what is available. In stead of a counter in your while loop, you could put the readLine() in the while statement in doStats.

If I do that I'll be duplicating the while loop found in readFile(). How can I modify the loop in readFile() so I'm not duplicating it? Sorry for all the questions, my instructor basically threw down a bunch of code and didn't explain it, so I'm kind of lost.

The loop in readFile is superfluous. All the min/max calculations are done on local variables iside doStats, so the loop in readFile is pretty much useless, unless there's some element of design and requirements that you haven't mentioned yet.

One more question. For some reason the program is not reading the first number in the file. No matter what I put there, the sum of the values stays the same as if the first number was 0. Does me initializing sum to 0 before the loop have any bearing on this?

EDIT: Putting a space before the number list in the file fixes the problem. Why is this so?

They're both correct syntax. I cut-and-pasted your code into my IDE and it works for me. I faked a data file, made the first line a -18, and added 12 lines of random integers. The -18 ended up in testInfo and the next line (which happened to be a 123) ended up in num. In the code you posted, num is never used for anything, but was being set. <shrug>