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 7 of 7

Thread: frustrating loop/array issue

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default frustrating loop/array issue

    Hey everyone!

    Fairly new to Java and have been struggling with some of the array use issues.

    I have 2 arrays the size of which is set by a SIZE variable:

    2 Dimensional

    Array 1: UserID[]
    Array 2 TransAmount[]

    Now I can loop through and printout the contents of these using a standard IF loop.

    What i'm having trouble with is looping through and printing out the number of transactions for each unique UserID WITHOUT using a sort/list function and without altering the data structure.

    What i've pretty much done to date is:
    for(i=0;i<size;i++)
          {
              System.out.print(UserID[i]);
              System.out.print(", ");
              System.out.println(TransAmount[i]);

    I would then try to setup a seperate For loop with a nested IF statement to basically increment a count value when comparing the previous userID with the next. and then print out the value etc. The problem is that this doesnt work for an unordered list where the userIDS appear multiple times. It also ends up just looping around and printing out every userid with the count.

    Anyway, a little help or pointer in the right direction would be appreciated. Its definitely an issue with my logic for the loop so id appreciate some advice.

    Would be so simple if i could sort the list etc.

    Cheers


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: frustrating loop/array issue

    Having 2 arrays is not the same as having one 2-dimensional array....
    Array 1: Userid[];
    Array 2: TransAmount[];
    Is two arrays.
    2-dimesional array: myArray[][];

    To access all members of a 2-dimensional array, you would use a nested loop setup. A for loop inside a for loop. The outer loop runs through the array of arrays, and the inner loop runs through each element of each array.

    Inside the inner loop, where you access each element one at a time, is the place to do the work. If the id appears multiple times, it will be printed multiple times. If that is undesired, you have to find a way to filter. This can be done many ways. Too many to list them all.

    You can sort the list... Again many ways to do that...

    With that being said:
    Do you have to use arrays?
    Why do you say "if i could sort the list"... why can you not?
    It looks to me like a key/value setup may be more suitable for what it looks like you are trying to do.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    0
    Thanked 7 Times in 4 Posts

    Default Re: frustrating loop/array issue

    Collections (Java 2 Platform SE v1.4.2)

    static void sort(List list)
    Sorts the specified list into ascending order, according to the natural ordering of its elements.
    static void sort(List list, Comparator c)
    Sorts the specified list according to the order induced by the specified comparator.

    Just to elaborate on sorting. Be sure to read up on documentation! If you don't understand how to read it, let us know.

  4. #4
    Junior Member
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: frustrating loop/array issue

    Hi guys,

    Unfortunately, the assignment for uni wont allow us to use sort functions. I can pretty much get all the way to the point where I can print out the multiple entries/lines etc. It seems like im going to have to filter the output in some manner to ensure that I just get the total transactions per user and not every line etc.

    So any ideas on how to filter the output without altering any data structures.

    Also my bad on the 2 dimensional arrays...your right they are flat single arrays etc.

    Thanks Guys,
    P.S Why they wont let us use sort is beyond me...I could have finished this in an hour otherwise....bloody uni

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: frustrating loop/array issue

    Actually thinking about it...I guess I could use a boolean to set to true after an id has been outputed and then an if statement to avoid printing it again

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: frustrating loop/array issue

    What about creating a new set of the data to be printed as you go through the arrays, and then printing your new data set after you have gone through them all. If the id exists in your new set, skip it and go to the next.

  7. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: frustrating loop/array issue

    Hi JPS,

    Thats exactly how I would do it but for some reason the wont allow us to create new array or dataset etc...crazy I know.

    anyway, it will clear it up if I post some code so here it is:

     
    int count = 0;
          int userID = 0;
          int c = 0;
          for (i=0;i<size;i++) //Main for loop
       	  {
        	userID = customerID[i];
     
     
        	for (c=0;c<size;c++)
        	{
     
        		  if(userID == customerID[c])
        		  {
        			  count++;
     
        		  } //end of if statement
     
     
     
     
        	}	  // End secondary FOR loop
     
     
        	System.out.print("Customer:" + userID);
    		  System.out.println(", Transactions:" + count);
    		  count = 0;
     
     
     
          } //End of main for loop

    I can get it to count and print out but what its doing is printing every single value from the array in the main loop and the count etc.

    So the question is...how do I filter the results printed to be only the user ID's once and not every listing in the array.

    I need to test for when to print and when not to print. I may get the same count for each customer 1 but each of these customer 1 ID's appear at a different place in the array. This is really the only difference so i need to test for it.

    Anyway, any ideas on how to test for this condition. I'm at a loss as to how to do it without breaking the assignment rules.

    All help appreciated
    Ideas??
    Last edited by knightsb78; August 12th, 2012 at 02:52 AM.

Similar Threads

  1. PDF iText code frustrating
    By RiseAboveYou in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 18th, 2011, 09:04 AM
  2. [SOLVED] Using for loop to calculate sine function (factorial issue)
    By Actinistia in forum Loops & Control Statements
    Replies: 2
    Last Post: March 11th, 2011, 03:38 PM
  3. Unending Loop Issue? Can't find it.
    By computerguy38 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 4th, 2011, 08:22 PM
  4. Basic loop issue
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 23rd, 2011, 05:10 PM
  5. Weird issue with while loop ending/being skipped
    By ang3c0 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 25th, 2009, 12:09 PM