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

Thread: Formatting 2d Array Output

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Formatting 2d Array Output

    I was wondering if there is a way to format the output of an array so that each indexed item has a discriptor and formatted in a certian way?

    Example:

    I have an array with 2 rows and 2 columns filled with numbers = double[][] numberArray = new double[2][2];

    sample data
    [0][0] = 1
    [0][1] = 1
    [1][0] = 2
    [1][1] = 2

    When I run my for loops my output statement is:

    for (int i=0; i<ROWS; i++)
    {
    for (int j=0; j<COLUMNS; j++)
    {
    System.out.print(" " + numberArray[i][j]);
    }

    This will output something like this:
    1.0 1.0
    2.0 2.0

    I would like to format the output to look like this:

    Day 1 Earnings $ 1.00
    Day 2 Earnings $ 2.00

    If I try the System.out.printf(.......); I can only put one discriptor and one format for both columns. I would like to format each column seperately from each other.

    Could you please point me in the right direction?

    Thanks,

    DS


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

    Default Re: Formatting 2d Array Output

    Please post the code you are having problems with. The printf() method should be able to print multiple values on multiple lines.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Formatting 2d Array Output

    The code that I need help with is:

    for (int i=0; i<ROWS; i++)
    {
    for (int j=0; j<COLUMNS; j++)
    {
    System.out.print(" " + numberArray[i][j]);
    }

    The problem is when I use the printf to format I don't know how to insert a discriptor in front of each column and format the numbers that are stored in the array.

    Here is an example:

    System.out.printf("Day %5.0f", numberArray[i][j]);

    it will output this:

    Day 1 Day 2

    I would like to make a format statement that would allow me to format for the [0][0] and then another format for [0][1] and all the way down the array.

    Thanks,

    DS

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

    Default Re: Formatting 2d Array Output

    Something like this?
    System.out.printf("Day %5.0f <Descriptor here> %5.0f\n", numberArray[i][j], numberArray[i][j+1]);

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Formatting 2d Array Output

    Thank you so much.

    I put in what you had up top and it would print the first line perfectly but it threw an error:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at arraywork.ArrayWork.main(ArrayWork.java:36)
    Java Result: 1

    I added a -1 behind the COLUMNS and now it works perfectly.
    Thank you

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

    Default Re: Formatting 2d Array Output

    My code was not intended to be usable as posted. It was just an idea of how to do it.

Similar Threads

  1. Formatting Output
    By mael331 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 14th, 2011, 06:41 PM
  2. Question about formatting output
    By mwr76 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 10th, 2011, 11:57 AM
  3. Store Values in 2D array and output to textarea
    By raverz1tildawn in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 7th, 2011, 03:13 PM
  4. Formatting output of a Double
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 11th, 2010, 04:20 PM
  5. Replies: 3
    Last Post: August 19th, 2009, 11:30 AM