setWidth string | Member | | Join Date: Aug 2007
Posts: 38
| |
I'm doing a simple program to learn java. I have the input and math parts right. It just isn't displaying right. It outputs a list of about 11 things-- with the format "string $ xx.xx"
In each line the '$'s should be aligned and the decimal point should be aligned.
This is what it currently shows-  - package excercise6;
-
import java.swing.*;
-
/**
-
*
-
* @author dye1107
-
*/
-
public class Main {
-
/* @param args the command line arguments
-
*/
-
public static void main(String[] args) {
-
String str, name;
-
Double gross, net;
-
Double fed, state, ss, medicare, pension;
-
final Double health=75.00;
-
-
name = JOptionPane.showInputDialog("What is your name: ");
-
str = JOptionPane.showInputDialog("What is your gross pay: ");
-
gross = Double.parseDouble(str);
-
-
fed = gross*0.15;
-
state = gross*0.035;
-
ss = gross*0.0575;
-
medicare = gross*0.0275;
-
pension = gross*0.05;
-
net = gross-fed-state-ss-medicare-pension-health;
-
-
-
JOptionPane.showMessageDialog(null, name + "\n" + String.format("%-40s", "Gross Amount:")+ "$" + String.format("%10.2f", gross) + "\n" + String.format("%-40s", "Federal Tax:") + "$" + String.format("%10.2f", fed) + "\n" + String.format("%-40s", "State Tax:") + "$"+ String.format("%10.2f", state) + "\n" + String.format("%-40s", "Social Security Tax:") + "$"+ String.format("%10.2f", ss) + "\n" + String.format("%-40s", "Medicare/Medicade Tax:") + "$"+ String.format("%10.2f", medicare) + "\n" + String.format("%-40s", "Pension Plan:") + "$"+ String.format("%10.2f", pension) + "\n" + String.format("%-40s", "Health Insurance:") + "$"+ String.format("%10.2f", health));
-
}
-
-
}
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: setWidth string
As you already saw tabs and spaces don't work. A JOptionPane accepts html text as well; you can put your text and figures in a table and display that instead of just a String.
kind regards,
Jos
| | Member | | Join Date: Aug 2007
Posts: 38
| | | re: setWidth string
yep, I did try '\t', but as I found, it didn't work.
I was almost certain that the - String.format("%-40s",string)
would have made each string take a length of 40. Could you tell me why it doesn't?
| | Member | | Join Date: Aug 2007
Posts: 38
| | | re: setWidth string
PS-- I'm pretty sure teacher doesn't expect us to do any HTML
Java Programming, 3rd Edition, ISBN 1-4239-0135-5
By DS Malik, Published by Thomson Course Technology
Chap 3
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: setWidth string Quote:
Originally Posted by jpenguin yep, I did try '\t', but as I found, it didn't work.
I was almost certain that the - String.format("%-40s",string)
would have made each string take a length of 40. Could you tell me why it doesn't? The default font of a JOptionPane is a proportional font (not all characters have the same width). You could try to change its font to, say, Courier New.
kind regards,
Jos
| | Member | | Join Date: Aug 2007
Posts: 38
| | | re: setWidth string
thanks, that was driving me crazy.
I set my JOptionPanes to use a MonoSpace font, and everthing lines up! :-)
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|