Simple Java formatting using string.format

Posted on 2010-09-15
Last Modified: 2013-11-23

It's a pretty basic question. Just trying to get the output to format the three rows on the message box. Maybe I am misunderstanding the width specifier...  
import javax.swing.JOptionPane;
import java.util.*;

public class PlaneRevCalc
  public static void main(String[] args) throws
String inputStr;
String outputStr;

//Class Names
String class1;
String class2;
String class3;

//Price per class
double pClass1;
double pClass2;
double pClass3;

//Passenger Qty by class
int qClass1;
int qClass2;
int qClass3;

//Calculations for output
double ttlRevPlane;

//Revenue by Class
double ttlRev1;
double ttlRev2;
double ttlRev3;

//Percent of Revenue by Class
double pctRev1;
double pctRev2;
double pctRev3;

double avgPrice;

//Read file from text. Expects a double

Scanner inFile = new Scanner(new FileReader("C:\\Users\\Scott\\My Dropbox\\school\\IDS201\\assn_1_plane\\price.txt"));

class1 =;
pClass1 = inFile.nextDouble();
class2 =;
pClass2 = inFile.nextDouble();
class3 =;
pClass3 = inFile.nextDouble();
//Get input from user
inputStr = JOptionPane.showInputDialog
    ("Enter the number of tickets sold for " + class1);
qClass1 = Integer.parseInt(inputStr);

inputStr = JOptionPane.showInputDialog
    ("Enter the number of tickets sold for " + class2);
qClass2 = Integer.parseInt(inputStr);

inputStr = JOptionPane.showInputDialog
    ("Enter the number of tickets sold for " + class3);
qClass3 = Integer.parseInt(inputStr);

ttlRev1 = qClass1 * pClass1;
ttlRev2 = qClass2 * pClass2;
ttlRev3 = qClass3 * pClass3;
ttlRevPlane =  ttlRev1 + ttlRev2 + ttlRev3;
pctRev1 = ttlRev1 / ttlRevPlane * 100;
pctRev2 = ttlRev2 / ttlRevPlane * 100;
pctRev3 = ttlRev3 / ttlRevPlane * 100;
avgPrice = ttlRevPlane / (qClass1 + qClass2 + qClass3);

outputStr = "Total Revenue for the Plane: " + ttlRevPlane + "\n" 
  +"Class Name          Revenue per section       % of Revenue Per Section \n"
  +"1234567890123456789012345678901234567890 \n"
  + String.format("%-30s|%30.2f|%10.2f%n",class1,ttlRev1,pctRev1)
  + String.format("%-30s|%30.2f|%10.2f%n",class2,ttlRev2,pctRev2)
  + String.format("%-30s|%30.2f|%10.2f%n",class3,ttlRev3,pctRev3)

    JOptionPane.showMessageDialog(null, outputStr, "Revenue Information:",

Open in new window

Question by:sukotto100
  • 3
LVL 92

Assisted Solution

objects earned 200 total points
ID: 33687387
your formatting looks fine
you need to use a fixed width font for that to work
LVL 26

Expert Comment

ID: 33689056
why don't you use JTable? simple and easy to use and format the way you want!
LVL 37

Accepted Solution

zzynx earned 300 total points
ID: 33689378
The code beneath gives me the attached screenshot
outputStr = "Total Revenue for the Plane: " + ttlRevPlane + "\n"
  + String.format("%-30s|%-30s|%-30s%n", "Class Name", "Revenue per section", "% of Revenue Per Section")
  + String.format("%-30s|%30.2f|%10.2f%n",class1,ttlRev1,pctRev1)
  + String.format("%-30s|%30.2f|%10.2f%n",class2,ttlRev2,pctRev2)
  + String.format("%-30s|%30.2f|%10.2f%n",class3,ttlRev3,pctRev3);

Font messageFont = new Font("Courier", Font.PLAIN, 12);
UIManager.put("OptionPane.messageFont", new FontUIResource(messageFont));

JOptionPane.showMessageDialog(null, outputStr, "Revenue Information:", JOptionPane.INFORMATION_MESSAGE);

Open in new window

LVL 37

Expert Comment

ID: 33692173
Thanx 4 axxepting
LVL 37

Expert Comment

ID: 33723968
Make that a 100/400 split and I agree.

>> you need to use a fixed width font for that to work
without giving any clue as how to do that imo doesn't earn that much points.
Trying to help people asking questions - especially in the "New to java" TA - is a matter of explaining as much as possible.
Being complete. Anticipating on next questions they might have and not waiting for them to ask the next question: "and how am I supposed to do that?"

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 114
hibernate insert example 13 34
running on tomcat not jboss eap 7.0 3 17
Setup GlassFish 4 21
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question