Welcome to Dream.In.Code
Become a Java Expert!

Join 149,534 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,530 people online right now. Registration is fast and FREE... Join Now!




wont print last element in array

 
Reply to this topicStart new topic

wont print last element in array, scanning for frequencies

stew_downunder
23 May, 2007 - 03:18 AM
Post #1

New D.I.C Head
Group Icon

Joined: 4 Apr, 2007
Posts: 17


Dream Kudos: 75
My Contributions
Just a simple problem here (which has got me stumped), I have constructed a program that scans an array for repeated numbers and then prints the quantity of each repeated number, but I can't seem to get it to print out the last array index (number 17 which would print the quantity as being 1). I am sure the problem is somewhere in my "for" loop. eg. for(int i=0,j=1,j<list.length,i++,j++)
but it prints everything except the last array index, I thought if maybe I went "j < list.length+1" but then it returns an arrayoutofbounds exception runtime error. I can't seem to find the in between of going out of bounds and stopping one short. I'm sure it must be a simple fix.

CODE
class Frequency
{
    public static void main(String[] args)
    {
        int list[] = {2, 2, 2, 3, 4, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 9, 17}; // construct array

        int count = 0; // counts individual elements
        int[] thisCount; // counts each group of same numbers

        for(int i = 0, j = 1; j < list.length; i++, j++) // scan the array
        {
            if(list[i] == list[j]) // if these two are the same
            {
                count++; // increment element count
            }
            else // otherwise
            {
                count++; // increment element count
                thisCount = new int[count]; // and put the group count in this array
                System.out.println(thisCount.length); // then print it
                count = 0; // and reset to zero for next iteration
            }
        }
    }
}


This post has been edited by stew_downunder: 23 May, 2007 - 03:20 AM
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Wont Print Last Element In Array
23 May, 2007 - 03:57 AM
Post #2

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions
Hi,

The problem is that with 2 counter comparing an element and the element above it, when you get the the last element, you can't compare with the next one, because there is no next one.

This is one way to get round it, but I'm sure there's others

CODE
class Frequency
{
    public static void main(String[] args)
    {
        int list[] = {2, 2, 2, 3, 4, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 9, 17}; // construct array

        int count = 0; // counts individual elements
        int[] thisCount; // counts each group of same numbers

        for(int i = 0, j = 1; i < list.length; i++, j++) // scan the array
        {
            if (j == 16)
            j = 0;
            if(list[i] == list[j]) // if these two are the same
            {
                count++; // increment element count
            }
            else // otherwise
            {
                count++; // increment element count
                thisCount = new int[count]; // and put the group count in this array
                System.out.println(thisCount.length); // then print it
                count = 0; // and reset to zero for next iteration
            }
        }
    }
}


Hope that helps icon_up.gif biggrin.gif
User is offlineProfile CardPM
+Quote Post

stew_downunder
RE: Wont Print Last Element In Array
23 May, 2007 - 04:24 AM
Post #3

New D.I.C Head
Group Icon

Joined: 4 Apr, 2007
Posts: 17


Dream Kudos: 75
My Contributions
Thanks Ellie and to the forum once again!

Works perfect now!

- Stew
User is offlineProfile CardPM
+Quote Post

Ellie
RE: Wont Print Last Element In Array
23 May, 2007 - 07:28 AM
Post #4

D.I.C Regular
Group Icon

Joined: 17 Jan, 2007
Posts: 427



Thanked: 4 times
Dream Kudos: 150
My Contributions
Great, you're most welcome smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 09:06PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month