-
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)
-
October 18th, 2012, 08:51 AM
#2
Re: Java programming help!! It compiles but wont run with out one error
THE ERROR MESSAGE IM RECEIVING IS
WE IN THEREWhiter 1WE IN THEREWhite 2Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Homework.main(Homework.java:48)
-
October 18th, 2012, 02:57 PM
#3
Re: Java programming help!! It compiles but wont run with out one error
Please use code tags when posting code.
The problem is caused by the array index being 5 when the maximum it can be is 4. The array has 5 elements indexed 0 to 4.
-
October 18th, 2012, 03:29 PM
#4
Re: Java programming help!! It compiles but wont run with out one error
Thanks for your response. What I am not understanding is, How do i fix this problem?
-
October 18th, 2012, 03:45 PM
#5
Re: Java programming help!! It compiles but wont run with out one error
No, what you are not understanding is that you need to understand what the problem is so you can fix it yourself.
Now I've told you what the problem is ie you are trying to access the element at index 5 of the array when the max allowable index is 4. So you now have to look at the code and find out why maxIndex is 5.
A good way of debugging this type of problem is to add some System.out.println("") statements and output the value of variables and various places so you can see how they are changing. I would help further but you haven't posted your code in code tags so it's difficult to read.
-
October 18th, 2012, 04:30 PM
#6
Re: Java programming help!! It compiles but wont run with out one error
Even considering I need help, the fact that your rude, Ill find some place else to receive help. Learn how to speak to people.
-
October 18th, 2012, 05:36 PM
#7
Re: Java programming help!! It compiles but wont run with out one error
Where was I rude? I was explaining what you need to do to fix this yourself. You'll never learn if other people just do it for you.
-
October 19th, 2012, 03:33 AM
#8
Re: Java programming help!! It compiles but wont run with out one error
You're accusing me of being rude! so lets just analyze your posts:
- You posted without reading the forum guides - you didn't use code tags.
- You typed part of your post in upper case letters - that is considered to be shouting.
- You basically ignored my reply:
- You neither went back and edited your post to include code tags, asked how to use them nor apologised for not using them.
- You made no discernible effort to solve the problem even though I told you what it was.
If all you wanted was to be told how to fix the problem and not how to debug it yourself, maybe you should change your user name from "teachme" to "tellme"
Last edited by keang; October 19th, 2012 at 12:49 PM.
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
|