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));
-
}
-
-
}
5 8994
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
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?
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
@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
thanks, that was driving me crazy.
I set my JOptionPanes to use a MonoSpace font, and everthing lines up! :-)
Sign in to post your reply or Sign up for a free account.
Similar topics
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......
|
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...
|
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.,...
|
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...
|
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...
|
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,...
|
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
|
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...
|
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?...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
|
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...
|
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,...
| |