This week's book giveaway is in the Testing forum.We're giving away four copies of The Way of the Web Tester: A Beginner's Guide to Automating Tests and have Jonathan Rasmusson on-line!See this thread for details.
Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Organizing an array

Brent Lancelot
Greenhorn
Posts: 8
hey im just having trouble with this array, its like it entirely misses the part where i swap the elements if one is bigger than the other and i cant figure it out

package Chapter5;

// Exercise 5.21
// This program adds the total hours from an array and organizes them from
// smallest to biggest and then prints the employee number and total hours
// from biggest to smallest

public class Total_Hours {
public static void main(String[] args)
{
// Declaring variables and arrays
int [][] hours = {{2,4,3,4,5,8,8},{7,3,4,3,3,4,4},{3,3,4,3,3,2,2},
{9,3,4,7,3,4,1},{3,5,4,3,6,3,8},{3,4,4,6,3,4,4},
{3,7,4,8,3,8,4},{6,3,5,9,2,7,9}};
int [][] totalhours = new int [8][2];
int x=0, y=0, max=0, maxIndex=0, number=0;

for(x=0; x<=7; x++)
{
for(y=0; y<=6; y++)
{
max = max + hours[x][y];
}
totalhours[x][0] = max;
totalhours[x][1] = x + 1;
max = 0;
}

// Sorting the results
for(x=7; x>=0; x--)
{
max = totalhours[x][0];
number = totalhours[x][1];
maxIndex = x;

for(y=0; y<=x; y++)
{
if(max < totalhours[y][0]);
{
max = totalhours[y][0];
number = totalhours[y][1];
maxIndex = y;
}
}
// swapping the values
if(maxIndex != x)
{
totalhours[maxIndex][0] = totalhours[x][0];
totalhours[maxIndex][1] = totalhours[x][1];
totalhours[x][0] = max;
totalhours[x][1] = number;
}
}

// Printing the results
for(x=7; x>=0; x--)
{
System.out.println("Employee: " + totalhours[x][1] + " Total Hours are : "
+ totalhours[x][0]);
}
}
}

i dont really know why its doing that but its probably something really stupid and simple, cud some1 help
[ April 05, 2006: Message edited by: Brent Lancelot ]

Ryan McGuire
Ranch Hand
Posts: 1085
4
Ohhhh.... this was a good one.

Try putting a println() inside the if() block where you compare max to totalhours[y][0]. It gets printed every time through the for(y) loop -- even when the comparison fails. Why is that??? What's wrong with the if statement that is causing the body to be executed even when the condition is false?
[ April 05, 2006: Message edited by: Ryan McGuire ]

Brent Lancelot
Greenhorn
Posts: 8
nah i still dont get it, is does print everytime but i dont know why
the syntax seems to be right so it should work i dont know tho

Brent Lancelot
Greenhorn
Posts: 8
cmon give me a better hint, u know like one that spells it out for me

Ryan McGuire
Ranch Hand
Posts: 1085
4
Originally posted by Brent Lancelot:
nah i still dont get it, is does print everytime but i dont know why
the syntax seems to be right so it should work i dont know tho

On the contrary, the problem IS just a syntax typo. Since you got all the other for and if statements correct, I don't think I'll be ruining any opportunities for learning if I just tell you the answer.

In the line with "if(max < totalhours[y][0])" you have a trailing semicolon. That semi is taken to be the end of the statement to execute if the condition is true. So the JVM does the comparison, if it's true it executes that empty statment, otherwise it's skips doing nothing. After either executing or skipping the empty statement, it executes those three statemeents in the "braced" block. The semicolon makes braces superfluous but not "wrong". Unfortunately the acceptability of those superfluous braces hides the fact that there is a statement between the closing parenthesis on the "if" line and the opening brace of that block of code.

Cool?
[ April 05, 2006: Message edited by: Ryan McGuire ]

Brent Lancelot
Greenhorn
Posts: 8
lol thats so stupid i didnt even see that semi colon