Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Simple Java formatting using string.format

Posted on 2010-09-15
6
Medium Priority
?
665 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 800 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 1200 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:
Suggested Courses

916 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