Java programming help!! It compiles but wont run with out one error
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Java programming help!! It compiles but wont run with out one error

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    4

    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)

  2. #2
    Join Date
    Oct 2012
    Posts
    4

    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)

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  4. #4
    Join Date
    Oct 2012
    Posts
    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?

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Oct 2012
    Posts
    4

    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.

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    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:
    1. You posted without reading the forum guides - you didn't use code tags.
    2. You typed part of your post in upper case letters - that is considered to be shouting.
    3. 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 code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center