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: Arrays sorting

  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 Arrays sorting

    I'm really stuck right now..
    like 2 hrs now, im tryna figure out a way to get this array in tabular format but its super difficult.
    dont really like asking for help but im always grateful for it..

      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} };
     
     
     
     
            System.out.printf("%20s\n\n", "Overall Sales Distribution");
     
            System.out.print("             ");
     
            for (int sp = 0; sp < totals[0].length; sp++)
                System.out.printf("SalesP%d  ", sp + 1);
     
            System.out.println("Total"); // total sales columns
     
            for(int prod = 0; prod < totals.length; prod++)
            {
                System.out.printf("Prod%d", prod + 1);
     
                for(int sp : totals[prod])
     
                    System.out.printf("%5d", sp);


  2. #2
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Arrays sorting

    What output are you expecting?

  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: Arrays sorting

    SalesP1 SalesP2 SalesP3 SalesP4
    prod1
    prod2 and then the array numbers here to match row n columns
    prod3
    prod4
    prod5

  4. #4
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Arrays sorting

    Just trial and error with manipulation of field width. I ran your code and it just comes out as:

    Overall Sales Distribution

    SalesP1 SalesP2 SalesP3 SalesP4 Total
    Prod1 500 150 575 500Prod2 200 700 175 200Prod3 300 75 100 400Prod4 100 185 118 250Prod5 600 260 663 345

    You are going to need a new line character in there somewhere if you want it to look the way you want it to.

    Are you having trouble with the field widths of the printf method or setting up the for loop? What exactly are you having trouble with.

  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: Arrays sorting

    setting up the loop i guess...
    i tried new line characters every where wont work.
    i've come to the conclusion now that i need a new loop or control statement
    to go to new line after reading every array row..
    but i'm having problem tryna do it..
    im not sure where to insert it or how do i do it..
    my teacher has no teaching skills wat so eva, im screwed

    --- Update ---

    setting up the loop i guess...
    i tried new line characters every where wont work.
    i've come to the conclusion now that i need a new loop or control statement
    to go to new line after reading every array row..
    but i'm having problem tryna do it..
    im not sure where to insert it or how do i do it..
    my teacher has no teaching skills wat so eva, im screwed

    --- Update ---

    sorry about the language

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,592
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Arrays sorting

    a way to get this array in tabular format
    Can you post an example of what you are trying to do
    and a sample of the program's current output.
    Wrap the post in code tags to preserve the spacing.
    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: Arrays sorting

    *(Total Sales) Use a two-dimensional array to solve the following problem: 
     * A company has four salespeople (1 to 4) who sell five different products (1 to 5). 
     * Once a day, each salesperson passes in a slip for each type of product sold. Each slip contains the following:
     
    a) The salesperson number
    b) The product number
    c) The total dollar value of that product sold that day
     
    Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information
    from all the slips for last month is available. Write an application that will read all this information
    for last month’s sales and summarize the total sales by salesperson and by product. All totals should
    be stored in the two-dimensional array sales. After processing all the information for last month,
    display the results in tabular format, with each column representing a particular salesperson and
    each row representing a particular product. Cross-total each row to get the total sales of each product
    for last month. Cross-total each column to get the total sales by salesperson for last month.
    Your tabular output should include these cross-totals to the right of the totaled rows and to the
    bottom of the totaled columns.



    thats the question

    and the code at the top page is how far ive gotten

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,592
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Arrays sorting

    I was asking for the program's output and a corrected version of that output.
    Can you post an example of what you are trying to do
    and a sample of the program's current output.
    Wrap the post in code tags to preserve the spacing.
    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: Arrays sorting

    current ouput:
    Overall Sales Distribution:
     
                 SalesP1  SalesP2  SalesP3  SalesP4  Total
    Prod1  500  150  575  500Prod2  200  700  175  200Prod3  300   75  100  400Prod4  100  185  118  250Prod5  600  260  663  345

    it should be:
                Salesp1      SalesP2     SalesP3     Salesp4    RowTotal
    prod1       500            150          575          500   
    prod2       200            700          175          200
    prod3       300             75           100          400
    prod4       100           185           118          250
    prod5       600           260           663          345 
     
    Col Total


    --- Update ---

    This is my code:
     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} };
     
     
     
     
            System.out.printf("%20s\n\n", "Overall Sales Distribution:");
     
            System.out.print("             ");
     
            for (int sp = 0; sp < totals[0].length; sp++)
                System.out.printf("SalesP%d  ", sp + 1);
     
            System.out.println("Total"); // total sales columns
     
            for(int prod = 0; prod < totals.length; prod++)
            {
                System.out.printf("Prod%d", prod + 1);
     
                for(int sp : totals[prod])
     
                    System.out.printf("%5d", sp);

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,592
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Arrays sorting

    Try adding some control characters in the Strings that are being printed.
    Add a "\n" where you want the following String to be on the next line
    Add a "\t" where you want the following String to be moved to the next "tab" column on the current line.

    Adding these control characters can take some experimenting.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    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: Arrays sorting

    i've tried all of these don't seem to work.. i was wondering if i need another control statement and where to put it

    --- Update ---

    ok thanks norm.. i tried adding new line somewhere i hadn't it worked..

    System.out.printf("\nProd%d", prod + 1);

    --- Update ---

    i hav the codes for row total and column total, but i hav no idea how to insert them into my code.

    row total:
    for(int row = 0; row < totals.length; row++)
            {
                int sum = 0;
                for(int column = 0; column < totals[row].length; column++)
                    sum += totals [row][column];

    Column total:
     for(int r = 0; r < totals.length; r++)
            {
                int total = 0;
                for(int c = 0; c < totals[r].length; c++)
                    total += totals [c][r];

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,592
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Arrays sorting

    row total and column total
    The row totals would go in a column at the end of each row
    The column totals would go in a row following the last row of details.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    LeeDD (March 4th, 2013)

  14. #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: Arrays sorting

    still seem that it has a liltle prob can u figure it out

       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
                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);
                }
     
                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);
                }
        }
    }

    here the ouput:
    n:
    		Overall Sales Distribution:
     
                SalesP1  SalesP2  SalesP3  SalesP4  Total    1725    1275     875     653    1868
    Prod1       500       150       575       500    1725    1275     875     653    1868
    Prod2       200       700       175       200    1725    1275     875     653    1868
    Prod3       300        75       100       400    1725    1275     875     653    1868
    Prod4       100       185       118       250    1725    1275     875     653    1868
    Prod5       600       260       663       345
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    Total      1100      1110       968      1350	at leedanielassignment3.TotalSales.main(TotalSales.java:84)
    Java Result: 1


    --- Update ---

    column total won't even add the row prod5 to the total

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,592
    Thanks
    50
    Thanked 2,235 Times in 2,207 Posts

    Default Re: Arrays sorting

    xception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at leedanielassignment3.TotalSales.main(TotalSales.ja va:84)
    The code at line 84 uses an index that is past the end of the array. Check why the index is too big.
    Remember array indexes range from 0 to the array length-1

    Check that the array indexes are being used in the correct order in the dimensions: row then column
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with arrays. Computing averages, sorting the array,
    By ribb3ntrop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 15th, 2012, 10:39 AM
  2. Need helpt with sorting arrays?
    By xion0374 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 19th, 2012, 06:00 PM
  3. Sorting Arrays
    By Bryan29 in forum Collections and Generics
    Replies: 5
    Last Post: November 28th, 2011, 06:21 AM
  4. Digital Watch program and Sorting arrays
    By c.P.u1 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 5th, 2011, 04:21 PM