setWidth string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jpenguin
    New Member
    • Aug 2007
    • 41

    setWidth string

    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-
    Code:
    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));
        }
    
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    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

    Comment

    • jpenguin
      New Member
      • Aug 2007
      • 41

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

      I was almost certain that the
      Code:
      String.format("%-40s",[B][I]string[/I][/B])
      would have made each string take a length of 40. Could you tell me why it doesn't?

      Comment

      • jpenguin
        New Member
        • Aug 2007
        • 41

        #4
        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

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by jpenguin
          yep, I did try '\t', but as I found, it didn't work.

          I was almost certain that the
          Code:
          String.format("%-40s",[B][I]string[/I][/B])
          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

          Comment

          • jpenguin
            New Member
            • Aug 2007
            • 41

            #6
            thanks, that was driving me crazy.

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

            Comment

            Working...