2

I'm new to java. I am trying to figure out how to format multiple arrays that are being filled in while the program is running (thanks to user input).

When I print the arrays, they do not format. I would use " " spaces, but the output of the arrays are dependent on what the user inputs. What I have is:

System.out.println("Order Summary:");
        System.out.println();
        System.out.println("Type                   Size          Quantity    Price");
        System.out.println("---------------------------------------------------------");
        for (i = 0; i < boolCount; i++)
        {
            System.out.println(typeArray[i] + sizeArray[i] + quantityArray[i] + priceArray[i]); //How to format?
        }

which would print out whatever is in the arrays without spaces. typeArray, sizeArray, and priceArray are String arrays, while quantityArray is an integer array.

I probably need to do System.out.printf(Enter code here) but I'm not sure how to do that with arrays. Any help would be appreciated.

EDIT: This is what I get if I add "\t" between the arrays:

System.out.println(typeArray[i] + "\t\t" + sizeArray[i] + "\t" + quantityArray[i] + "\t" + priceArray[i]);

Order Summary:
Type                   Size          Quantity    Price
---------------------------------------------------------
BBQ Chicken     Large   2   $25.98
Chicken-Bacon Ranch     Personal    22  $175.78
Meat Lovers     Extra Large 33  $791.67

Order total: $993.43

---------------------------------------------------

EDIT: This is what I get with:

System.out.printf("%-23.23s %-14.14s %-12.12d %-5.5s", typeArray[i], sizeArray[i], quantityArray[i], priceArray[i]);

Order Summary:

Type                   Size          Quantity    Price
---------------------------------------------------------
Exception in thread "main" java.util.IllegalFormatPrecisionException: 12
    at java.util.Formatter$FormatSpecifier.checkInteger(Formatter.java:2984)
    at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2729)
    at java.util.Formatter.parse(Formatter.java:2560)
    at java.util.Formatter.format(Formatter.java:2501)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at pizzamenu.PizzaMenu.main(PizzaMenu.java:372)
Java Result: 1
6
  • Could you try appending tab characters "\t" between the values ? Commented Feb 2, 2016 at 16:37
  • Actually didn't think of that, give me a second to try Commented Feb 2, 2016 at 16:38
  • How about defining a maximum field length n, write a method that pads up to n or truncates down to n, then concatenate those length modified tokens with spaces? Commented Feb 2, 2016 at 16:39
  • What I am trying to do is an order summary after user orders pizza. Some of the 'specialty pizzas' have long names/short names which messes up the "\t" between the code, will edit answer to show Commented Feb 2, 2016 at 16:43
  • 2
    I think this would be the answer. stackoverflow.com/a/14421916/2961429 Commented Feb 2, 2016 at 16:44

1 Answer 1

1

You can use format specifiers.

Format specifiers follow the following format:

%[flags][width][.precision][argsize]typechar

So in your case, you would do the following:

System.out.printf("%-23s %-14s %-12d %-5s", typeArray[i], sizeArray[i], quantityArray[i], priceArray[i]);

More info on format specifiers: https://sharkysoft.com/archive/printf/docs/javadocs/lava/clib/stdio/doc-files/specification.htm

Sign up to request clarification or add additional context in comments.

9 Comments

Gave me bunch of errors during run, do I need to import a class?
You said typeArray and sizeArray are String arrays, quantityArray is int arrays, and priceArray is double array?
typeArray, sizeArray, and priceArray are all string arrays. quantityArray is int. priceArray is a string array becauase I am adding "$" to priceArray[i]. I can just as easily change it to a double array and print the "$" seperately if that would make it easier
Oh okay, I just assumed price was a double. I changed the type of priceArray[i] to be string.
Still giving me errors. Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer + others.
|

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.