-
October 18th, 2012, 08:49 AM
#1
Java programming help!! It compiles but wont run with out one error
Cslab.javaCslab.java
import java.util.*;
public class Homework
{
private static String getLastNames(String fullname){
int space = fullname.indexOf(' ');
String lastname = fullname.substring(space+1);
return lastname;
}
public static void main(String[] args)
{
String[] names = {"barry White", "Crazy Hannah", "Steven Tyler", "Bobby Brown", "Matt Ryan"};
String[] lnames = new String[names.length];
String[] uniquelname = new String [lnames.length];
int[] sCount = new int [uniquelname.length];
int count=0 ;
int maxCount=0;
int maxIndex=0;
int dynamicSize = 0;
boolean itIsThere;
//maxCount= [0];
for(int i = 0; i<names.length; i++)
{
lnames[i] = getLastNames(names[i]);
//if (maxCount < Lnames[i])
}
for (String cName : lnames)
{
for(int i = 0; i < uniquelname.length; i++)
{
if(cName.equals(uniquelname[i]))
{
//we want it to increase counter one
sCount[i]++;
itIsThere = true;
break;
}else{
itIsThere = false;
}
if (!itIsThere)
{
uniquelname[dynamicSize] = cName;
dynamicSize++;
}
}
for(int i = 0; i<sCount.length; i++)
{
if (maxCount < sCount[i])
{
maxCount = sCount[i];
maxIndex = i;
}
}
System.out.print("WE IN THERE" + uniquelname[maxIndex] + " " + (sCount[maxIndex] +1 ) );
}
}
}
THE ERROR MESSAGE IM RECEIVING IS
WE IN THERECormier 1WE IN THERECormier 2Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Homework.main(Homework.java:48)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|