Reputation: 1
So I am VERY new to programming. I take an online course and I understand the code I have inputted but, it doesn't work properly. I constantly get a "index out of bounds" error, which doesn't make sense since I only kept 5 values from 0-4. Can you identify the problem? Thanks!
public class LEGGO
{
public static void main(String args[])
{
int j, i, l, m;
int This[] = new int[5];
This[0] = 8;
This[1] = 4;
This[2] = 24;
This[3] = 14;
This[4] = 56;
for (j=1; j<5; j++)
{
for(l=0; l<5-j; l++)
{
if (This[l]<This[l+1])
{
i=This[l];
This[l]=This[l+1];
This[l+1]=i;
}
}
}
for(m=0; m<5; m++);
System.out.print(This[m]);
}
}
Upvotes: 0
Views: 89
Reputation: 625
Welcome to programming, I hope you are enjoying the challenge.
You have made a syntax error. The line for(m=0; m<5; m++); should actually be for(m=0; m<5; m++){ an you have missed a closing brace } .
This is quite unlucky because it means the program behaves very differently from what you might expect - but the java compiler is doing what it is told.
In java (and many other programming languages) the semi colon means 'end of statement'. This means that because you have put one after the for loop that what happens is that it just executes the for loop without doing anything else. This means that the variable m gets counted up to 5. Having done that the program then proceeds to try read the array -> This[m] which is This[5]. As the array This is five elements long, and arrays start counting at zero the element This[4] is the last element of your array.
I have used comments to label the closing braces in your program so it is easier to see what was wrong. If you use a java IDE is makes seeing problems like this easier.
This however is a dubious programming lesson - as while it does teach you how arrays and for statements are used, it has long been recognized that you shouldn't code like this as it causes these kind of problems. While not a beginner's programming subject an experienced programmer would have avoided making this mistake using the [Java collection's api] (http://docs.oracle.com/javase/7/docs/technotes/guides/collections/overview.html).
public class LEGGO{
public static void main(String args[]){
int j, i, l, m;
int This[] = new int[5];
This[0] = 8;
This[1] = 4;
This[2] = 24;
This[3] = 14;
This[4] = 56;
for (j=1; j<5; j++){
for(l=0; l<5-j; l++){
if (This[l]<This[l+1]){
i=This[l];
This[l]=This[l+1];
This[l+1]=i;
}// end if
} //end for l
} //end for j
for(m=0;m<5;m++){
System.out.print(This[m]);
}// end for m
}//end main
}//end class
Happy java hacking.
Upvotes: 0
Reputation: 2526
You have a semicolon after your last for loop delcaration, remove it and it will work....
for(m=0; m<5; m++)
Upvotes: 0
Reputation: 681
Your stack trace will tell you the line that is causing the exception.
Edit: The problem is the semicolon at the end of the for loop and lack of '{'. When you perform m++, it will increment m to 5, and then be used in your println.
You can also avoid these types of problems through reduced variable scope. If you initialize m inside the for loop, it would have not been within scope on the following line.
Also, follow java code conventions for better readability: http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html
Upvotes: 1