File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Sorting array on command line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting array on command line" Watch "Sorting array on command line" New topic
Author

Sorting array on command line

JDemon519
Greenhorn

Joined: Aug 12, 2002
Posts: 1
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

Joined: Dec 10, 2001
Posts: 7023
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ken Cobbs
Greenhorn

Joined: Jun 18, 2002
Posts: 29
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting array on command line
 
Similar Threads
Large arrays
Help me with matrix...please!!!!!!!
sorting an array
Array in reverse order......?
Sorting two dimensional array