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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 115
HTTPSessionBindingListernter not configured in deployment descriptor 2 53
even odd program using while loop 3 40
arguments to jar 5 26
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

772 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