Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting array on command line

 
JDemon519
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program is a simple Bubble Sort but I am getting an index.outOfBounds exception when trying to run the program.I put 7 for array.length but it can be any number.Here is my program
public class BubbleSort{
public static void bubbleSort( int b[] )
{
for(int pass = 1; pass < b.length; pass++) // passes
for(int i = 0; i < b.length - 1; i++) // one pass
if( b[i] > b[i + 1] ) // one comparison
swap(b, i, i + 1); // one swap
}

// swap two elements of the array
public static void swap( int c[], int first, int second )
{
int hold; // temp holding area for swap
hold = c[first];
c[first] = c[second];
c[second] = hold;
}
public static void main(String[] args){
int s = Integer.parseInt(args[7]);
int d[] = new int [s];
bubbleSort(d);
for(int i = 0; i < d.length; i++){
System.out.println(d[i]);
}
}
}
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JDemon519,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
--------------
Also...
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.
Note that you have the ability to edit your own posts - just click on the icon that looks like a piece of paper.
 
Ken Cobbs
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that "int s = Integer.parseInt(args[7]);" is only loading the 8th argument entered. If you enter less than 8 arguments you will get the "out of range" error. I assume your probably wanting to load all of the arguments keyed in. Think you may have to load all of the arguments into the array. I think it's "arg.length" that will tell you how many args there are.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic