My Java Array program that is suppose to have a bubble sort -


so new programming. take online course , understand code have inputted but, doesn't work properly. "index out of bounds" error, doesn't make sense since kept 5 values 0-4. can identify 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;     (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]);     } } 

your stack trace tell line causing exception.

edit: problem semicolon @ end of loop , lack of '{'. when perform m++, increment m 5, , used in println.

you can avoid these types of problems through reduced variable scope. if initialize m inside loop, have not been within scope on following line.

also, follow java code conventions better readability: http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -