Solved

Simple Java formatting using string.format

Posted on 2010-09-15
6
646 Views
Last Modified: 2013-11-23
Hi,

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.io.*;
import java.util.*;

public class PlaneRevCalc
{
  public static void main(String[] args) throws
     FileNotFoundException
  {
            
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 = inFile.next();
pClass1 = inFile.nextDouble();
class2 = inFile.next();
pClass2 = inFile.nextDouble();
class3 = inFile.next();
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);

//Calculations
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);

//Output  
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:",
                                    JOptionPane.INFORMATION_MESSAGE);
    System.exit(0);
  }
}

Open in new window

9-15-2010-11-33-06-AM.png
0
Comment
Question by:sukotto100
  • 3
6 Comments
 
LVL 92

Assisted Solution

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

Expert Comment

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

Accepted Solution

by:
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

ScreenShot169.png
0
 
LVL 37

Expert Comment

by:zzynx
ID: 33692173
Thanx 4 axxepting
0
 
LVL 37

Expert Comment

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

Saying
>> 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?"
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights2  challenge 7 76
triangle challenge 4 77
groupSum6 challenge 6 38
java set up 1 46
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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now