2

How can I possibly print this into table form.

this is my code:

 public class TdArrayPrice {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String [] flvr= {"","Cookies n' Cream","Okinawa         ","Dark Chocolate  ","Wintermelon     
        ","Matcha          "};
        double [][] price={ {0,0,0,},
                            {0,100.00,50.00},
                            {0,125.00,75.00},
                            {0,120.00,65.00},
                            {0,120.00,65.00},
                            {0,120.00,65.00}};
        
            for(int i=1; i<6; i++) {
                System.out.println("["+i+"] "+flvr[i]);
                for(int j=1; j<price[i].length; j++) {
                out.print(+price[i][j]+"\t");
            }
            }
        
    }

}

I want my output to be look like this: [1]: https://i.sstatic.net/dWtXV.png I hope you guys could help me, cause I've been trying to work with it for like tons of days.

1
  • You should read about printf(). (System.out.printf()) Or Format. Commented May 23, 2021 at 1:55

2 Answers 2

1

Fixing functionality

I really liked your attempt to solve the problem, it was almost correct. Let's see what was wrong:

for (int i = 1; i < 6; i++) {
    System.out.println("["+i+"] "+flvr[i]);
    for (int j = 1; j < price[i].length; j++) {
        System.out.print(+price[i][j]+ "\t");
    }
}
  1. In the first print, you're using println, so the prices will be one line below of the coffee name. Change it to print.

  2. After you print the prices, you're not adding a new line, so the next coffee and its prices will be printed in the same line as the previous one. Add a println to the outer loop.

Fixed code:

for (int i = 1; i < 6; i++) {
    System.out.print("["+i+"] "+flvr[i]);
    for (int j = 1; j < price[i].length; j++) {
        System.out.print(+price[i][j]+ "\t");
    }
    System.out.println();
}

Output:

[1] Cookies n' Cream100.0   50.0    
[2] Okinawa125.0    75.0    
[3] Dark Chocolate120.0 65.0    
[4] Wintermelon120.0    65.0    
[5] Matcha120.0 65.0    

Formatting improvements

You can use System.out.printf to add some padding to the coffee name and prices instead of '\t':

// Print the header
System.out.printf("%-23s%10s %11s\n", "MILKTEA FLAVORS", "Large", "Small");
for (int i = 1; i < 6; i++) {
    // Add a right padding of 20 spaces in the coffee name
    System.out.printf("[%d] %-20s", i, flvr[i]);
    for (int j = 1; j < price[i].length; j++) {
        // Add a left padding of 10 spaces in each price
        System.out.printf("%10.2f ", price[i][j]);
    }
    System.out.println();
}

New output:

MILKTEA FLAVORS             Large       Small
[1] Cookies n' Cream        100.00      50.00 
[2] Okinawa                 125.00      75.00 
[3] Dark Chocolate          120.00      65.00 
[4] Wintermelon             120.00      65.00 
[5] Matcha                  120.00      65.00 

Data representation

Since Java is an object-oriented language, you can use objects to represent each coffee.

class Coffee {
    final String name;
    final double largePrice;
    final double smallPrice;

    public Coffee(String name, double largePrice, double smallPrice) {
        this.name = name;
        this.largePrice = largePrice;
        this.smallPrice = smallPrice;
    }
}

Instantiating them:

final Coffee[] coffees = {
    new Coffee("Cookies n' Cream", 100.00, 50.00),
    new Coffee("Okinawa", 125.0, 75.0),
    new Coffee("Dark Chocolate", 120.0, 65.0),
    new Coffee("Wintermelon", 120.0, 65.0),
    new Coffee("Matcha", 120.0, 65.0),
};

Printing them:

for (int i = 0; i < coffees.length; i++) {
    final Coffee coffee = coffees[i];
    System.out.printf("[%d] %-20s", i, coffee.name);
    System.out.printf("%10.2f ", coffee.largePrice);
    System.out.printf("%10.2f", coffee.smallPrice);
    System.out.println();
}
Sign up to request clarification or add additional context in comments.

2 Comments

THANK YA'LLLLL GUYSS!!! I REALLY DO APPRECIATE YA'LL. I JUST STARTED LEARNING NEW THINGS IN PROGRAMMING. YOU GUYS ARE TRULY AWESOME :>
That's really nice to hear, hope you have a great future in programming. If my answer helped you in any way, you can always accept it.
0

Check out the power of String.format() in detail. It's worth it - regardless if you use printf or String.format or even other programming languages like C/C++ printf statements.

https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#format(java.lang.String,java.lang.Object...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.