Connecting Tech Pros Worldwide Help | Site Map

setWidth string

Member
 
Join Date: Aug 2007
Posts: 38
#1: Sep 11 '09
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-


Expand|Select|Wrap|Line Numbers
  1. package excercise6;
  2.     import java.swing.*;
  3. /**
  4.  *
  5.  * @author dye1107
  6.  */
  7. public class Main {
  8.      /* @param args the command line arguments
  9.      */
  10.     public static void main(String[] args) {
  11.     String str, name;
  12.     Double gross, net;
  13.     Double fed, state, ss, medicare, pension;
  14.     final Double health=75.00;
  15.  
  16.     name = JOptionPane.showInputDialog("What is your name:  ");
  17.     str = JOptionPane.showInputDialog("What is your gross pay:  ");
  18.     gross = Double.parseDouble(str);
  19.  
  20.     fed = gross*0.15;
  21.     state = gross*0.035;
  22.     ss = gross*0.0575;
  23.     medicare = gross*0.0275;
  24.     pension = gross*0.05;
  25.     net = gross-fed-state-ss-medicare-pension-health;
  26.  
  27.  
  28.     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));
  29.     }
  30.  
  31. }
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Sep 11 '09

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
#3: Sep 11 '09

re: setWidth string


yep, I did try '\t', but as I found, it didn't work.

I was almost certain that the
Expand|Select|Wrap|Line Numbers
  1. 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
#4: Sep 11 '09

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
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: Sep 11 '09

re: setWidth string


Quote:

Originally Posted by jpenguin View Post

yep, I did try '\t', but as I found, it didn't work.

I was almost certain that the

Expand|Select|Wrap|Line Numbers
  1. 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
#6: Sep 11 '09

re: setWidth string


thanks, that was driving me crazy.

I set my JOptionPanes to use a MonoSpace font, and everthing lines up! :-)
Reply