If anyone is familar with programming in java..

Support, Discussion, Reviews
Post Reply
User avatar
Akaran_D
Way too much time!
Way too much time!
Posts: 4151
Joined: July 3, 2002, 2:38 pm
Location: Somewhere in my head...
Contact:

If anyone is familar with programming in java..

Post by Akaran_D »

Could you drop me a PM?
Have a bug that's been ticking me off for days that I can't seem to work around.

edit: Specifically, while/for loop & a file reading issue with a null pointer exception error.
Akaran of Mistmoore, formerly Akaran of Veeshan
I know I'm good at what I do, but I know I'm not the best.
But I guess that on the other hand, I could be like the rest.
User avatar
Tenuvil
Way too much time!
Way too much time!
Posts: 1841
Joined: July 11, 2002, 6:13 pm

Post by Tenuvil »

post code pls.
User avatar
Akaran_D
Way too much time!
Way too much time!
Posts: 4151
Joined: July 3, 2002, 2:38 pm
Location: Somewhere in my head...
Contact:

Post by Akaran_D »

Code: Select all

public static void LoadFile()
{
 String Filename;
 TextInputFile LoadedFile;
 Stdout.println("Enter file to load...");
 Filename = Stdin.readLine();
 LoadedFile = new TextInputFile(Filename);
 
 Index = new String[100];
 VIDA = new int[100];
 VPA = new int[100];
 VAA = new int[100];
 VGA = new int[100];

while (!LoadedFile.eof())
{
 for (int i = 0; i <= 100; i++)
  {
  
    VIDA[i] = LoadedFile.readInt();
    VPA[i] = LoadedFile.readInt();
    VAA[i] = LoadedFile.readInt();
    VGA[i] = LoadedFile.readInt();
    Stdout.println(VIDA[i]);
    Stdout.println(VPA[i]);
    Stdout.println(VAA[i]);
    Stdout.println(VGA[i]);
   }
   
   Stdout.println("End of file");
   LoadedFile.close();
  }

}
I'm getting mutiple "reading past end of file" errors. I think I've got it traced to the i = 100, but when I set it up so that it reads all the lines in the file and sets that value to a variable called "FileSize" and then set i <= Filesize, it throws out another IO exception.. Null pointer, I think.

I'm only a first semester programmer and my teacher is a brilliant man but can't explain his way out of a plastic bag sometimes. He hates to give us actual code, wants us to 'figure it out' on our own using what we've found that works.

edit: If I remove the For loop, it's fine, and doesn't attempt to read past the file. However, then it doesn't scale the i values for the arrays.
Akaran of Mistmoore, formerly Akaran of Veeshan
I know I'm good at what I do, but I know I'm not the best.
But I guess that on the other hand, I could be like the rest.
User avatar
Tenuvil
Way too much time!
Way too much time!
Posts: 1841
Joined: July 11, 2002, 6:13 pm

Post by Tenuvil »

try getting rid of the for/next loop and increment i manually in the while loop.

it seems that if you have less than 100 records in your input file you'd hit eof without the outer while (!eof)/do loop ever seeing eof.

why 100?
User avatar
Akaran_D
Way too much time!
Way too much time!
Posts: 4151
Joined: July 3, 2002, 2:38 pm
Location: Somewhere in my head...
Contact:

Post by Akaran_D »

Answer:
Because that is the length my professor told me to do it. I spent two hours figuring out an algorithm that would calculate the number of lines in the file (each data entry is 4 lines, each line needs to go into an array), showed it to him, and he's like, "Oh, just take that out and set the value to 100 and be done with it".

I'll give that a try right now.

edit: That worked, thanks Ten. Enjoy the VV's.
Akaran of Mistmoore, formerly Akaran of Veeshan
I know I'm good at what I do, but I know I'm not the best.
But I guess that on the other hand, I could be like the rest.
User avatar
Tenuvil
Way too much time!
Way too much time!
Posts: 1841
Joined: July 11, 2002, 6:13 pm

Post by Tenuvil »

WOOT, Thanks Akaran!

:)
User avatar
Llaffer
Star Farmer
Star Farmer
Posts: 451
Joined: October 18, 2002, 3:43 am

Post by Llaffer »

I'm not familiar with java, but if it is similar to C, your "for" should have been :

for (i = 0; i < 100; i++)

vs. i <= 100

You're arrays were defined as 100 (which is numbered 0-99)

So your 0 through 100 inclusive is 101 elements where element "100" isn't defined since max is 99.

Don't know if this was your problem for java or not, but if you want to try to go back to your using "for", try removing the "=".
User avatar
Lohrno
Way too much time!
Way too much time!
Posts: 2416
Joined: July 6, 2002, 4:58 pm
Location: California
Contact:

Post by Lohrno »

That's what I said, but I deleted my message...

i = 0; i<=100 will result in 101 iterations.

-=Lohrno
User avatar
Tenuvil
Way too much time!
Way too much time!
Posts: 1841
Joined: July 11, 2002, 6:13 pm

Post by Tenuvil »

Why would you need the inner loop anyway? Since processing is stopped by the eof condition, and all you need to do is increment the counter to fill the array nodes, it's inelegant to have a separate for loop inside the while (!eof) loop.

Course what do I know, I'm not a programmer, I'm an auditor :)
User avatar
Lohrno
Way too much time!
Way too much time!
Posts: 2416
Joined: July 6, 2002, 4:58 pm
Location: California
Contact:

Post by Lohrno »

Tenuvil wrote:Why would you need the inner loop anyway? Since processing is stopped by the eof condition, and all you need to do is increment the counter to fill the array nodes, it's inelegant to have a separate for loop inside the while (!eof) loop.
Agreed, but I'm not going to coach him on style in a language I don't know. =D
Course what do I know, I'm not a programmer, I'm an auditor :)
You audit code? In that case you are probably more qualified. =D Oh the horrors I bet you've seen! =D

-=Lohrno
Lynks
Way too much time!
Way too much time!
Posts: 2774
Joined: September 30, 2002, 6:58 pm
XBL Gamertag: launchpad1979
Location: Sudbury, Ontario

Post by Lynks »

Well I am a programmer. One possibility is that once you enter you eof loop, you begine to enter your second loop for 100 times. If by any chance you have 1 left over, you re-enter that enter for/next loop and read 100 more times, even though you might only of had 1 line to read.

Or, this might happen:
- Enter EOF loop
- Read 100 times
- Closes file.
- EOF checks to see if File is EOF but it cannot (since its closed) and returns an error


You might run into some problems later on so it would be wise to get rid of it.
Last edited by Lynks on November 23, 2004, 5:43 pm, edited 1 time in total.
User avatar
Tenuvil
Way too much time!
Way too much time!
Posts: 1841
Joined: July 11, 2002, 6:13 pm

Post by Tenuvil »

heh, I keep the fact that I can audit code a secret from my corporate masters, otherwise I'd be looking at an 80 hour workweek. :D

I audit the IT environment, generally limited to information security, program change process, system development methodologies, and operations procedures. If you've heard of Sarbanes-Oxley or 404 reviews, that's what I do.
User avatar
Ashur
Way too much time!
Way too much time!
Posts: 2604
Joined: May 14, 2003, 11:09 am
Location: Columbus OH
Contact:

Post by Ashur »

SOX SUX
- Ash
User avatar
Tenuvil
Way too much time!
Way too much time!
Posts: 1841
Joined: July 11, 2002, 6:13 pm

Post by Tenuvil »

Ashur wrote:SOX SUX
indeed it do but it pays people like me a fucking king's ransom
User avatar
Ashur
Way too much time!
Way too much time!
Posts: 2604
Joined: May 14, 2003, 11:09 am
Location: Columbus OH
Contact:

Post by Ashur »

Yep, no doubt.

I suppose I should mention I work in a J2EE shop for a large bank.
- Ash
User avatar
Tenuvil
Way too much time!
Way too much time!
Posts: 1841
Joined: July 11, 2002, 6:13 pm

Post by Tenuvil »

I feel your pain. People in banking have had to deal with the Money Laundering regs, Privacy Act, Patriot Act and its inherent contradictions to the Privacy Act, and now SOx. It's a wonder more banks don't say "fuck it" and change to become, say, dry cleaners. Privately held dry cleaners at that.
Post Reply