Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort an Array(need help)

Status
Not open for further replies.

tbarnes2788

Programmer
Feb 26, 2004
7
US
I wrote this program using the sortString method. When I run the program I get this error:

Exception in thread "main" java.lang.ArrayIndexOutofBoundsException at HairSalon.main<HairSalon.java:18>

What does this mean? Did I write the code right? Code is below.



public class HairSalon
//Program will allow reports to be output sorted by each type of
//service offered
{
public static void main(String[] args)
{
String[] services = {"Cut", "Shampoo", "Manicure", "Style",
"Permanent", "Trim"};
int x;
System.out.println("Before sort");
for(x = 0; x < 6; ++x)
System.out.println(services[x]);
sortStrings(services, services.length);
System.out.println("/nAfter sort");
for(x = 0; x < 6; ++x);
System.out.println(services[x]);
}

public static void sortStrings(String[] array, int len)
{
int a,b;
String temp;
int highSubscript = len - 1;
for(a = 0; a < highSubscript; ++a)
for(b = 0; b < highSubscript; ++b)
if(array.compareTo(array[b + 1]) >0)
{
temp = array;
array = array[b + 1];
array[b = 1] = temp;
}

}

}
 
public class HairSalon
//Program will allow reports to be output sorted by each type of
//service offered
{
public static void main(String[] args)
{
String[] services = {"Cut", "Shampoo", "Manicure", "Style",
"Permanent", "Trim"};
int x;
System.out.println("Before sort");
for(x = 0; x < 6; x++)
System.out.println(services[x]);
sortStrings(services, services.length);
System.out.println("/nAfter sort");

for(x = 0; x < 6; x++)
System.out.println(services[x]);


}

public static void sortStrings(String array[], int len)
{
int a,b;
String temp;
int highSubscript = len - 1;
for(a = 0; a < highSubscript; a++)
{
for(b = a; b < highSubscript; b++) // b = a
{
if(array.compareTo(array[b+1]) >0)
{
temp = array;
array = array[b+1];
array[b + 1] = temp; // b+1
}
}
}
}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top