Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need help using Arrays!

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Need help using Arrays!

    I have been working on this code for awhile now and I almost have it except I cant figure out how to get rid of the "java.lang.ArrayIndexOutOfBoundsException" at lines 21 & 43(shown In parentheses). I have to generate 100 random numbers between 0-9 and count how many times each number comes up. Can I not take this approach or is there something that needs tweaked?? My full code is below, thank-you!

    public static void main(String[] args) {
    // TODO code application logic here
    int [] numbers = createArray();
    System.out.println("The Numbers are:");
    displayArray(numbers);
    (21) int[] counts = countNumbers(numbers);[/COLOR]
    System.out.println();
    System.out.println("The occurence of each number are: ");
    displayCounts(counts);
    }
    public static int[] createArray(){
    int[] numbers = new int[100];
    for(int i=0; i<numbers.length; i++)
    numbers[i]= (int)(Math.random()*10);
    return numbers;
    }
    public static void displayArray(int[] numbers){
    for (int i = 0; i<numbers.length; i++){
    if((i+1)%20 == 0)
    System.out.println(numbers[i]);
    else
    System.out.print(numbers[i] + " ");
    }
    }
    public static int[] countNumbers(int[] numbers){
    int[] counts = new int[10];
    for (int i =0; i <numbers.length; i++)
    (43) counts[numbers[i] + '0']++;
    return counts;
    }
    public static void displayCounts(int[] counts){
    for (int i =0; i<counts.length; i++){
    if ((i+1)%10 == 0)
    System.out.println(counts[i] + " " + (int)(i + '0'));
    else
    System.out.println(counts[i] + " " + (int)(i + '0') + " ");
    }
    }
    }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Need help using Arrays!

    If you are getting an ArrayIndexOutOfBoundsException with "counts[numbers[i] + '0']++;" then it might be a good idea to check with System.out.println() which array index you are trying to access.

    public static int[] countNumbers(int[] numbers){
        int[] counts = new int[10];
        for (int i =0; i <numbers.length; i++) {
            System.out.println("About to access counts array at index " + (numbers[i] + '0'));
            counts[numbers[i] + '0']++;
        }
        return counts;
    }

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help using Arrays!

    The indexes producing the problem seem to change each time the program runs, but it always seems to be in the range of indexes 48-55. What does this tell me?

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Need help using Arrays!

    Quote Originally Posted by asb23698 View Post
    The indexes producing the problem seem to change each time the program runs, but it always seems to be in the range of indexes 48-55. What does this tell me?
    Well, the first thing it tells you is what you already knew: you have a problem! Because the array itself only has 10 slots in it so index values in the range 48-55 will break things.

    So basically your formula numbers[i]+'0' for calculating the index is wrong and has to be changed. You are wanting to calculate an index value in the correct range: zero to nine.

    Think about what counts is supposed to represent, and what index you should use for a given element in the numbers array. Perhaps more System.out.println() might help. (Remember while you're debugging the program you don't actually need 100 numbers.)

    public static int[] countNumbers(int[] numbers){
        int[] counts = new int[10];
        for (int i =0; i <numbers.length; i++) {
            System.out.println("Found " + numbers[i] + " in the numbers array")
            System.out.println("About to update counts array at index " + (numbers[i] + '0'));
            counts[numbers[i] + '0']++;
        }
        return counts;
    }

    What you are looking for is something more appropriate than numbers[i]+'0' to use as an array index.

Similar Threads

  1. Can Anyone Help With Arrays?
    By metaleddie13 in forum Collections and Generics
    Replies: 9
    Last Post: November 16th, 2011, 12:12 AM
  2. Arrays
    By ruffu054 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 7th, 2011, 11:18 PM
  3. Help with arrays
    By mike2452 in forum Collections and Generics
    Replies: 6
    Last Post: August 6th, 2011, 12:15 PM
  4. Need Help with Arrays
    By zennbang in forum Object Oriented Programming
    Replies: 3
    Last Post: July 31st, 2011, 06:41 AM
  5. 2d arrays help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 27th, 2010, 05:27 PM

Tags for this Thread