This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Help with Beginning Java Class Assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with Beginning Java Class Assignment" Watch "Help with Beginning Java Class Assignment" New topic
Author

Help with Beginning Java Class Assignment

Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
I'm not asking for someone to do the assignment, I'm just stuck and need a kick in the right direction. The teacher has provided a .class file named MyCsvFile.class and a folder named Data that contains piecesCompletedbyEmployee,v00.txt. I trust these two files are correct. I can email the complete files, teacher provided psuedo code etc if neccessary. My coding for my file - compiles, but when I run it, I get an exception error "java.lang.ArrayIndexOutOfBoundsException" pointing to my method call for ProducePieceWorkCalculationReport.

When I debug, I get "getUncaughtExceptionHandler().uncaughtException(this, e); pointing to the thisEmployeePerPiecePayRate = PerPiecePayRate(piecesCompleted[index]); line.

here's my code, and again I can email the complete passage if needed. I'm thinking that I'm not passing the variable index through to the method correctly, but I'm not sure, the "guidelines" instruct that it is a local variable of the main method.

I'm coming back in after I've got it fixed and deleting all but the relevant code. Don't want a classmate to be able to google an A for the assignment. Thanks again for all your help


[ April 04, 2008: Message edited by: Richard Chambers ]
Edwin Keeton
Ranch Hand

Joined: Jul 10, 2002
Posts: 214

I don't see the error right away but I can say that ArrayIndexOutOfBounds is a runtime exception, which means that the compiler won't check for it since at compile time it can't tell whether the array index is too big or not.

What's happening is that the index loop variable is larger than the size of the array. I don't see why, but I would guess that somehow the completedPieces (?) array isn't being initialized the way you're expecting.

You might have already checked that out, I'm just trying to jiggle your thinking a little bit to help you get started.


SCJP, SCWCD
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

You've got something odd going on here, immediately before the "problem" line...

Note the semicolon after the parenthesis. This is an "empty" statement that executes with each iteration of your loop. The code in the braces that follow is only executed once, after the for loop finishes (by which time, index is out of bounds).
[ April 03, 2008: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
Thanks Marc,

I think my problem very well may be in that line.

The pseudo code for that is:

FOR index FROM zero TO (employeeName.length-1) BY 1

my code is just so you don't have to scroll back)

for
(index = 0; index <= (employeeName.length - 1); index++);




I think I have the code right, but maybe the grouping in the FOR loop wrong, possibly in the IF statements. Also, the instructions have us defining a local INT named index in the main method, but not initializing it. Is there a way to pass it to the ProducePieceworkCalculationReport method without declaring it a second time in that method? I think that could be causing me problems too.

I sure could use some help. This is the eighth of ten program assignments in the class and I've gotten the max points on all of them so far, thought I was getting a good grasp, but on this one I'm lost. The other people in the class are still working on program four, so they're no help and the teacher tells us to figure it out ourselves, don't bother him.

SO PLEASE HELP FOLKS

I'll be glad to email the included (teacher provided) files and instructions by email to anyone that wants to look at the whole package.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Richard Chambers:
...for
(index = 0; index <= (employeeName.length - 1); index++); ...

The problem is that semicolon after the parenthesis. It's an empty statement, which has the effect of saying...

for (index = 0; index <= (employeeName.length - 1); index++) [do nothing];

Then after you've "done nothing" for each iteration of the loop, execution proceeds to...

thisEmployeePerPiecePayRate = PerPiecePayRate(piecesCompleted[index]);

But by that time, "index" is out of bounds because you've finished iterating.

Remove that semicolon.
[ April 03, 2008: Message edited by: marc weber ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Richard Chambers:
...the instructions have us defining a local INT named index in the main method, but not initializing it. Is there a way to pass it to the ProducePieceworkCalculationReport method without declaring it a second time in that method? I think that could be causing me problems too...

You're confusing yourself. There are two separate "index" variables here, and they have nothing to do with each other, so there's no reason to pass this from one method to another. It would be less confusing to give that variable an entirely different name in ProducePieceworkCalculationReport. In fact, the variable is only used inside the loop in that method, so you would be best off declaring it there, narrowing its scope to just the loop...

for(int x = 0; x <= (employeeName.length - 1); x++) {...
Richard Chambers
Ranch Hand

Joined: Apr 03, 2008
Posts: 40
Thanks for everyones help.

Now I feel dumb. I spent a LOT of hours staring at this.

I removed the semi-colon and it got me past that problem. I added index in the paramater list that I pass to the method and it's now working. Sort of. Still a formatting error, but I can figure that out.

Thanks again
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I really don't think you want to pass "index" as a parameter. It might not hurt anything, but (from the code that was previously posted) I don't see that it would do anything.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Not to take anything away from what's been said so far, but to add to it: in the "for" loop, the upper bound is being determined by the length of the employeeName array; then in the line that's giving the error, it's being applied to the piecesCompleted array. It's entirely possible that this is just fine, if and only if the two arrays are always guaranteed to have the same length. If they're not, then even if you get the stray semicolons under control, things still aren't going to work right. Just want to make sure you understand this, is all.


[Jess in Action][AskingGoodQuestions]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Ernest Friedman-Hill:
...in the "for" loop, the upper bound is being determined by the length of the employeeName array; then in the line that's giving the error, it's being applied to the piecesCompleted array...

Good catch -- I didn't even notice that!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with Beginning Java Class Assignment