473,320 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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-


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. }
Sep 10 '09 #1
5 8986
JosAH
11,448 Expert 8TB
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
Sep 11 '09 #2
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?
Sep 11 '09 #3
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
Sep 11 '09 #4
JosAH
11,448 Expert 8TB
@jpenguin
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
Sep 11 '09 #5
thanks, that was driving me crazy.

I set my JOptionPanes to use a MonoSpace font, and everthing lines up! :-)
Sep 11 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Krakatioison | last post by:
My sites navigation is like this: http://www.newsbackup.com/index.php?n=000000000040900000 , depending on the variable "n" (which is always a number), it will take me anywhere on the site......
5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
0
by: Rob | last post by:
I've tried to use the SetWidth method on a column in a programmatically generated Word table. But I get a runtime error -- Value out of range. I've tried everything I can think of but I'm...
3
by: Vaxius | last post by:
After trying to compile the following code, gcc gives me error: 'setwidth' was not declared in this scope I'm not sure why it would do this, since iomanip is included. Am I missing something?...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.