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

Thread: Array Looping

  1. #1
    Junior Member LeeDD's Avatar
    Join Date
    Feb 2013
    Location
    Caribbean
    Posts
    27
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Array Looping

    I Cant Seem To Figure Out Where To Put My Loop For Row Totals.. It Ends Up With Number Repetitions n errors.. the code in italics is supposed to represent row totals but i tried placing it every where but i keep getting a constant inappropriate result..

      public static void main(String[] args)
        {
     
            int [] [] totals  = { {500, 150, 575, 500}, 
                                {200, 700, 175, 200}, 
                                {300, 75, 100, 400}, 
                                {100, 185, 118, 250}, 
                                {600, 260, 663, 345} };
     
     
     
          // header
            System.out.printf("\t\t%20s\n\n", "Overall Sales Distribution:");
     
            System.out.print("            ");
     
            // column headers for table
            for (int sp = 0; sp < totals[0].length; sp++)
            {
     
                System.out.printf("SalesP%d  ", sp + 1);
            }
     
            System.out.print("Total"); // total sales columns
     
            // row headers for table
            for(int prod = 0; prod < totals.length; prod++)
             {  
     
                 // print row totals in column total
               [I] for(int row  = 0; row < totals.length; row++)
                {
                    int sum = 0;
     
                    for(int column = 0; column < totals[row].length; column++)
                    {
                    sum += totals [row][column];
     
                    System.out.printf("%8d", sum);
                    }[/I]
     
                }
                System.out.printf("\nProd%d", prod + 1);
     
                // print array values in table
                    for(int sp : totals[prod])
                    {
     
                        System.out.printf("%10d", sp);          
                    }
             }
     
                System.out.print("\nTotal");
     
              for(int r = 0; r < totals.length; r++)
                {
                    int total = 0;
                    for(int c = 0; c < totals[r].length; c++)
                        total += totals[c][r];
     
                        System.out.printf("%10d", total);
                }
     
    }
     
     
    }


    --- Update ---

    This Is The result i get

    Overall Sales Distribution:
     
                SalesP1  SalesP2  SalesP3  SalesP4  Total     500     650    1225    1725     200     900    1075    1275     300     375     475     875     100     285     403    
    Prod1       500       150       575       500     500     650    1225    1725     200     900    1075    1275     300     375     475     875     100     285     403     653  
    Prod2       200       700       175       200     500     650    1225    1725     200     900    1075    1275     300     375     475     875     100     285     403     653    
    Prod3       300        75       100       400     500     650    1225    1725     200     900    1075    1275     300     375     475     875     100     285     403     653     
    Prod4       100       185       118       250     500     650    1225    1725     200     900    1075    1275     300     375     475     875     100    
    Prod5       600       260       663       345
    Total      1100      1110       968      1350


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Array Looping

    Can you post what you want the output to look like? And add some comments saying what is wrong with the program's current output.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member LeeDD's Avatar
    Join Date
    Feb 2013
    Location
    Caribbean
    Posts
    27
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Array Looping

    Salesp1 salesp2 salesp3 salesp4 Total

    prod1 500 150 575 500 1725
    prod2 200 700 175 200 1225
    prod3 300 75 100 400 875
    prod4 100 185 118 250 653
    prod5 600 260 663 345 1868

    total 1100 1110 968 1350



    the column total works ok.. but i cant seem to get the row total to work.. i tried placing the code in different places but i just wont work

    --- Update ---

    Salesp1 salesp2 salesp3 salesp4 Total

    prod1 500 150 575 500 1725
    prod2 200 700 175 200 1225
    prod3 300 75 100 400 875
    prod4 100 185 118 250 653
    prod5 600 260 663 345 1868

    total 1100 1110 968 1350



    the column total works ok.. but i cant seem to get the row total to work.. i tried placing the code in different places but i just wont work

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Array Looping

    I think it'd be easier to completely rewrite the code rather than try to fix what is there.

    Pseudo code:
    print the header
    begin loop through rows
    print the row header in first column
    begin loop through columns
    print the column data w/o endline
    end loop thru columns
    print endline
    end loop thru rows
    print total line
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member LeeDD's Avatar
    Join Date
    Feb 2013
    Location
    Caribbean
    Posts
    27
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Array Looping

    ok then thanks.. i'll tell you whats up with it after im done

    --- Update ---

    Can i ask what exactly do u mean by end line.. i dont wanna be unsure

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Array Looping

    The endline character: "\n"or what is printed by the println() method.

    What I'm talking about is the line.separator value. Its value depends on what OS you are on. On Windows its: "\r\n" or others it can be "\r" and others "\n".

    Many programs on Windows will recognize "\n" as the end of a line. But not all do.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member LeeDD's Avatar
    Join Date
    Feb 2013
    Location
    Caribbean
    Posts
    27
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Array Looping

    oh aight.. i kno it as new line

    --- Update ---

    i loop thru the row with this

    for( int row = 0; row < array.length; row++)

    and apparently it keeps printing column header more than once. which then gives me more than 1 table.

    public static void main(String[] args)
        {
            int [] [] totals  = { {50, 15, 75, 50}, 
                                {20, 70, 15, 20}, 
                                {30, 75, 10, 40}, 
                                {10, 85, 81, 50}, 
                                {60, 60, 66, 45} };
     
     
     
          // header
            System.out.printf("\t\t%20s\n\n", "Overall Sales Distribution:");
     
     
                 // looping thru rows
                for(int row  = 0; row < totals.length; row++)
                {
                            System.out.print("             ");
     
                            // loop n print column headers
                     for (int sp = 0; sp < totals[0].length; sp++)
                        { 
                            System.out.printf("SalesPerson%d  ", sp + 1);
                        }
     
                             // print row total last in row column
                            System.out.print("Total"); // total sales columns
     
                    int sum = 0; // initiate sum
     
                    // looping thru columns 
                    for(int column = 0; column < totals[row].length; column++)
                    {
                         for(int prod = 0; prod < totals.length; prod++)
                         {  
                            System.out.printf("\nProd%d", prod + 1);
     
     
                                // print array values in table
                                for(int sp : totals[prod])
                                    {
     
                                        System.out.printf("%10d", sp);          
                                    }
                        }
                                sum += totals [row][column];
     
                                System.out.printf("%8d", sum);
                    } 
                    System.out.println("");
     
                }
     
        }


    --- Update ---

    hers what the results look like:

    		Overall Sales Distribution:
     
                 SalesPerson1  SalesPerson2  SalesPerson3  SalesPerson4  Total
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45      50
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45      65
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45     140
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45     190
                 SalesPerson1  SalesPerson2  SalesPerson3  SalesPerson4  Total
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45      20
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45      90
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45     105
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45     125
                 SalesPerson1  SalesPerson2  SalesPerson3  SalesPerson4  Total
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45      30
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45     105
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45     115
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45     155
                 SalesPerson1  SalesPerson2  SalesPerson3  SalesPerson4  Total
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45      10
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45      95
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45     176
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45     226
                 SalesPerson1  SalesPerson2  SalesPerson3  SalesPerson4  Total
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45      60
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45     120
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45     186
    Prod1        50        15        75        50
    Prod2        20        70        15        20
    Prod3        30        75        10        40
    Prod4        10        85        81        50
    Prod5        60        60        66        45     231

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Array Looping

    Why are the the loops nested 3 deep?
    Here's what should work:
    One loop for each row
    one loop for the columns on each row

    What are the other loops for?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member LeeDD's Avatar
    Join Date
    Feb 2013
    Location
    Caribbean
    Posts
    27
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Array Looping

    Sum of them are for the headers.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Array Looping

    Try reducing the nesting depth to 2.

    Write some pseudo code for how the code works
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Feb 2013
    Location
    N. Ireland
    Posts
    29
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: Array Looping

    It might help to indent your code properly. You can read it better. But anyway this is what I have been able to do :
      **** Please don't spoonfeed code
    Last edited by Norm; March 18th, 2013 at 08:03 PM. Reason: Removed code

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Array Looping

    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member LeeDD's Avatar
    Join Date
    Feb 2013
    Location
    Caribbean
    Posts
    27
    My Mood
    Relaxed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Array Looping

    ooooooh

  14. #14
    Junior Member
    Join Date
    Feb 2013
    Location
    N. Ireland
    Posts
    29
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: Array Looping

    Quote Originally Posted by Norm View Post
    Read and understood. Sorry.

    --- Update ---

    Quote Originally Posted by LeeDD View Post
    ooooooh
    I should have known better.

Similar Threads

  1. java 2d array is not looping right. i dont understand whyy and how to fix it
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 25th, 2013, 07:13 AM
  2. A little help with looping my array
    By tyneframe in forum Loops & Control Statements
    Replies: 4
    Last Post: November 24th, 2012, 10:41 AM
  3. Need help with looping!
    By crsoccerplayer6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 04:09 PM
  4. Looping through an Array.
    By kl2eativ in forum Loops & Control Statements
    Replies: 4
    Last Post: June 10th, 2011, 07:15 PM
  5. Looping through a multidimensional array
    By MysticDeath in forum Loops & Control Statements
    Replies: 2
    Last Post: October 11th, 2009, 05:41 PM