G. Bea G. Bea - 6 months ago 17
Java Question

What is the simplest but most effective way in formatting strings into a chart in Java?

For my program, I need to display the item description, quantity, and price in a chart-like format. So that means that description of item 1 would be in line with its price and quantity. So far, I've tried several methods I found on the Internet but haven't succeeded. I am using Dr. Java so please suggest something that is compatible with that compiler.
Thank you in advance!

Here's what I have so far:

public static void main(String []args){
Scanner input=new Scanner(System.in);
String sentinel = "End";

String description[] = new String[100];

int quantity[] = new int[100];

double price [] = new double[100];
int i = 0;
// do loop to get input from user until user enters sentinel to terminate data entry
do
{
System.out.println("Enter the Product Description or " + sentinel + " to stop");
description[i] = input.next();

// If user input is not the sentinal then get the quantity and price and increase the index
if (!(description[i].equalsIgnoreCase(sentinel))) {
System.out.println("Enter the Quantity");
quantity[i] = input.nextInt();
System.out.println("Enter the Product Price ");
price[i] = input.nextDouble();
}
i++;
} while (!(description[i-1].equalsIgnoreCase(sentinel)));


// companyArt();
//System.out.print(invoiceDate());
//System.out.println(randNum());



System.out.println("Item Description: ");
System.out.println("-------------------");
for(int a = 0; a <description.length; a++){
if(description[a]!=null){
System.out.println(description[a]);
}
}
System.out.println("-------------------\n");


System.out.println("Quantity:");
System.out.println("-------------------");
for(int b = 0; b <quantity.length; b++){
if(quantity[b]!=0){
System.out.println(quantity[b]);
}
}
System.out.println("-------------------\n");

System.out.println("Price:");
System.out.println("-------------------");
for(int c = 0; c <price.length; c++){
if(price[c]!=0){
System.out.println("$"+price[c]);
}
}
System.out.println("-------------------");

//This is where I multiply the price and quantity together to get the total
double total = 0.0;
for (int j = 0; j < quantity.length; j++){

total += quantity[j] * price[j];
}

if(total != 0){
System.out.println("Total: " + total);
}
}
}

Answer

Your code prints out a list of item descriptions, followed by a list of quantities, followed by a list of prices. I'm assuming this is not how you want it to look. The best solution would be to use a single for loop that prints each of these three things out per line. The following code prints out a table with three columns labelled "Item Description", "Quantity", and "Price", with each row representing an item, and dashed lines separating each row:

System.out.println("Item Description:\tQuantity:\tPrice:\t");
System.out.println("---------------------------------------------------------");
for(int a = 0; a <description.length; a++){
    if (description[a].equalsIgnoreCase("end")) {
        break;
    }
    System.out.println(description[a] + "\t\t" + quantity[a] + "\t\t" + price[a]);
    System.out.println("---------------------------------------------------------\n");
}  

The output will be formatted like this:

Item Description:   Quantity:   Price:  
---------------------------------------------------------
asdfaoiew;rjlkf     248         4309.0
---------------------------------------------------------

asodifaasd          43          2323.0
---------------------------------------------------------

asdfasoif           234         2.0
---------------------------------------------------------

If the columns don't line up properly, you'll need to either add or remove one "\t" after the description, depending on how long or short your item descriptions are.