Results 1 to 7 of 7
  1. #1
    gnawk is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Count and Display

    Hi i need to make a method that does the example below
    if the array contains these number
    -12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12

    The output should be:
    N Count
    4 2
    3 3
    2 2
    1 4
    -1 1
    -12
    Java Code:
     public static void countAndDisplay(int[] array) throws IOException
        {
            System.out.println("Count statistics:");
            System.out.println("    N  Count");
            int current = array[0];
            for (int ndx = 1; ndx < array.length; ndx++)
            {
                int temp = 1;
                while (array[ndx] == current)
                {
                    temp++;
                    ndx++;
                }
                System.out.printf("%5d %6d\n", current, temp);
                current = array[ndx];
            }
        }
    however my out come is
    Count statistics:
    N Count
    4 2
    3 2
    2 1
    1 3
    -1 0
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 16
    at ArrayProcessing.countAndDisplay(ArrayProcessing.ja va:135)
    at ArrayProcessing.main(ArrayProcessing.java:32)
    Last edited by gnawk; 03-08-2012 at 04:49 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Count and Display

    One thing you could do is print the argument array so that you know what it contains when that method is called.

    Then step through the method by hand paying attention to the values that temp and ndx are given. Pay special attention to ndx because it looks a bit suspicious when a loop variable is changed in a loop like that.

  3. #3
    gnawk is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: Count and Display

    im totally lost right now.... how would i print the argument array so that i can check what it contains and such??
    i would really appreciate any help i can get on this project

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Count and Display

    how would i print the argument array
    Java Code:
    for(int ele :array) {
        System.out.print(ele + " ");
    }
    System.out.println();

  5. #5
    gnawk is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: Count and Display

    i found out why it is not working about i don't know how to fix it.... since the while loop tries to check array[16] even though it doesn't exists.
    how do i make sure the while loop doesn't try to access an index that's out of bounds??

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Count and Display

    Use an if statement to check that ndx does not exceed the limi before accessing array[16]. Or include this as part of a while statement:

    Java Code:
    while(something && somethingElse) {
        // code here
    }
    In a while block like that if something is false then somethingElse is not evaluated. This is called "shortcircuit" evaluation and is very handy when you want to check that an index is within bounds or a variable is nonnull before evaluating some condition using that index or variable.

  7. #7
    gnawk is offline Member
    Join Date
    Feb 2012
    Posts
    17
    Rep Power
    0

    Default Re: Count and Display

    i thought that this makes sure that ndx increment until array.length
    for (int ndx = 1; ndx < array.length; ndx++)

    i don't get the while block you mentioned up there
    but for if

    Java Code:
    if (ndx == array.length)
    break;
    i made this and this works for some cases but still gives

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 16
    at ArrayProcessing.countAndDisplay(ArrayProcessing.ja va:164)
    at ArrayProcessing.main(ArrayProcessing.java:53)

    but it works all the outputs are correct

    Original array:
    156 23 19 13 8 7 5 4 2


    Count statistics:
    N Count
    156 1
    23 1
    19 1
    13 1
    8 1
    7 1
    5 1
    4 1


    it doesn't print out 2 and doesn't give out any error
    Last edited by gnawk; 03-08-2012 at 05:56 AM.

Similar Threads

  1. Replies: 1
    Last Post: 12-01-2010, 07:35 PM
  2. display count down timer
    By wizekraker in forum NetBeans
    Replies: 7
    Last Post: 11-12-2010, 08:46 PM
  3. Replies: 1
    Last Post: 05-31-2010, 09:46 AM
  4. display histogram that count white pixels
    By TamTam in forum Java 2D
    Replies: 6
    Last Post: 02-14-2009, 10:26 PM
  5. Replies: 8
    Last Post: 12-29-2008, 08:12 AM

Posting Permissions

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