tbarnes2788
Programmer
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;
}
}
}
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;
}
}
}