473,396 Members | 1,895 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,396 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 8994
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.