Solved

Simple Java formatting using string.format

Posted on 2010-09-15
6
652 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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
numbers ascending pyramid 101 264
ejb entity bean example issue 2 43
printf performancy 11 103
How to log java errors in tomcat 8 37
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

734 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