DreamWeaver777
asked on
Having trouble with converting numbers in java to english text.
Hi I am a complete newbie to the java scene and I am wanting to know more about java. Right now I am having trouble designing a simple program that will accept any double numerical value from 0.00 to 999999.99 and convert it to english text. For example my input into my GUI would be 7856.89 and I need the out put on my GUI should come out as Seven Thousand Eight Hundred Fifty Six Dollars and 89 Cents.
the layout should look something like this of the model using these methods(only public items allowed everything else must be private), and I have the GUI already completed:
public class MoneyModel {
public MoneyModel() */null constructor set initial value to 0.00
public double getMoneyAmount()
public void setMoneyrAmount(double)
public String getMoneyrWords()
Public static void main(String[] args) */contain test code
Your help is greatly appreciated! Thanks!
the layout should look something like this of the model using these methods(only public items allowed everything else must be private), and I have the GUI already completed:
public class MoneyModel {
public MoneyModel() */null constructor set initial value to 0.00
public double getMoneyAmount()
public void setMoneyrAmount(double)
public String getMoneyrWords()
Public static void main(String[] args) */contain test code
Your help is greatly appreciated! Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
dollars and cents are at the bottom
ASKER
here is a copy of the GUI, this may make it a little more clear:
public class MoneyGUI extends JFrame implements ActionListener {
private MoneyModel theModel;
/**
* JButton to let the stuff go
*/
private JButton goButton;
/**
* JTextArea to get the input from the user
*/
private JTextArea inputArea;
/**
* JTextArea to show the output to the user
*/
private JTextArea outputArea;
/**
* Default constructor to setup the window
* @since 1.0
*/
public MoneyGUI() {
//For the application setup the window
if (this instanceof JFrame) {
setSize(500,200);
setLocation(40,40);
setTitle("Check Writer");
setDefaultCloseOperation(E XIT_ON_CLO SE);
}
//Initialize instance variables
theModel = new MoneyModel();
goButton = new JButton("Go");
goButton.addActionListener (this);
inputArea = new JTextArea(1,10);
outputArea = new JTextArea(3,40);
outputArea.setEditable(fal se);
outputArea.setLineWrap(tru e);
outputArea.setWrapStyleWor d(true);
//Setup top panel
JPanel topPanel = new JPanel();
JLabel numLabel = new JLabel("Number to translate: ");
topPanel.add(numLabel);
topPanel.add(inputArea);
//Setup center panel
JPanel centerPanel = new JPanel();
JLabel outLabel = new JLabel("Number as text: ");
centerPanel.add(outLabel);
centerPanel.add(outputArea );
this.getContentPane().add( topPanel,B orderLayou t.NORTH);
this.getContentPane().add( centerPane l,BorderLa yout.CENTE R);
this.getContentPane().add( goButton,B orderLayou t.SOUTH);
}
public void actionPerformed (ActionEvent e) {
try {
String inText = inputArea.getText();
double inValue = Double.parseDouble(inText) ;
theModel.setCheckAmount(in Value);
outputArea.setText(theMode l.getMoney Words());
}
catch (NumberFormatException except) {
outputArea.setText("You have input a non-numeric value");
inputArea.setText("");
}
}
/**
* Main method to get stuff started
* @since 1.0
*/
public static void main (String args[]) {
MoneyGUI myGUI = new MoneyGUI();
myGUI.show();
}
}
public class MoneyGUI extends JFrame implements ActionListener {
private MoneyModel theModel;
/**
* JButton to let the stuff go
*/
private JButton goButton;
/**
* JTextArea to get the input from the user
*/
private JTextArea inputArea;
/**
* JTextArea to show the output to the user
*/
private JTextArea outputArea;
/**
* Default constructor to setup the window
* @since 1.0
*/
public MoneyGUI() {
//For the application setup the window
if (this instanceof JFrame) {
setSize(500,200);
setLocation(40,40);
setTitle("Check Writer");
setDefaultCloseOperation(E
}
//Initialize instance variables
theModel = new MoneyModel();
goButton = new JButton("Go");
goButton.addActionListener
inputArea = new JTextArea(1,10);
outputArea = new JTextArea(3,40);
outputArea.setEditable(fal
outputArea.setLineWrap(tru
outputArea.setWrapStyleWor
//Setup top panel
JPanel topPanel = new JPanel();
JLabel numLabel = new JLabel("Number to translate: ");
topPanel.add(numLabel);
topPanel.add(inputArea);
//Setup center panel
JPanel centerPanel = new JPanel();
JLabel outLabel = new JLabel("Number as text: ");
centerPanel.add(outLabel);
centerPanel.add(outputArea
this.getContentPane().add(
this.getContentPane().add(
this.getContentPane().add(
}
public void actionPerformed (ActionEvent e) {
try {
String inText = inputArea.getText();
double inValue = Double.parseDouble(inText)
theModel.setCheckAmount(in
outputArea.setText(theMode
}
catch (NumberFormatException except) {
outputArea.setText("You have input a non-numeric value");
inputArea.setText("");
}
}
/**
* Main method to get stuff started
* @since 1.0
*/
public static void main (String args[]) {
MoneyGUI myGUI = new MoneyGUI();
myGUI.show();
}
}
:o\ hmm... the Class "MoneyModel", is that a personal class? or have you downloaded an API? Because, I don't know of this class, and my compiler won't compile your program because of it..
[r.D]
[r.D]
Hrmmm... I'm sorry to say that you may have to do it manually and it won't be a fun class to write. :(
You'll have to have a class to check which numbers are located in what digits. This is probably done best by converting your double to a String. Then you can use String.charAt(0) (for the first digit) and String.length() to help you figure out where things are. Then you'll just have to make all the '7's into "Sevens" and '5's into "Fives", etc. Note that you'll need to do something special for all the "teen" numbers. (i.e., if the second to last integer digit is a 1, check whether the last digit, is one, two, three, etc. and print out, eleven, twelve, thirteen, etc.). Besides that you just check where the digit is and tack on the unit i.e. if the digit is the first of 4 integer digits, tack on "Thousand". Good luck!
You'll have to have a class to check which numbers are located in what digits. This is probably done best by converting your double to a String. Then you can use String.charAt(0) (for the first digit) and String.length() to help you figure out where things are. Then you'll just have to make all the '7's into "Sevens" and '5's into "Fives", etc. Note that you'll need to do something special for all the "teen" numbers. (i.e., if the second to last integer digit is a 1, check whether the last digit, is one, two, three, etc. and print out, eleven, twelve, thirteen, etc.). Besides that you just check where the digit is and tack on the unit i.e. if the digit is the first of 4 integer digits, tack on "Thousand". Good luck!
Follow the link in my first comment. That is how to generate English (and French) names of numbers.
"Don't re-invent the wheel when you can borrow one from your neighbour."
"Don't re-invent the wheel when you can borrow one from your neighbour."
ASKER
Hey guys I really appreciate your comments and suggestions ya'll have been an enormous help. I just have one more last thing to ask about this.
Can anybody give me any suggestions on how to implement this code that I have into each of my methods? (Thanks for the page Rick)
methods:
public class MoneyModel {
public MoneyModel() */null constructor set initial value to 0.00
public double getMoneyAmount()
public void setMoneyrAmount(double)
public String getMoneyrWords()
Public static void main(String[] args) */contain test code
code:(I know I will have to change my class names)
class EnglishDecimalFormat {
private static final String[] majorNames = {
"",
" Thousand",
" million",
" billion",
" trillion",
" quadrillion",
" quintillion"
};
private static final String[] tensNames = {
"",
" Ten",
" Twenty",
" Thirty",
" Fourty",
" Fifty",
" Sixty",
" Seventy",
" Eighty",
" Ninety"
};
private static final String[] numNames = {
"",
" One",
" Two",
" Three",
" Four",
" Five",
" Six",
" Seven",
" Eight",
" Nine",
" Ten",
" Eleven",
" Twelve",
" Thirteen",
" Fourteen",
" Fifteen",
" Sixteen",
" Seventeen",
" Eighteen",
" Nineteen"
};
private String convertLessThanOneThousand (int number) {
String soFar;
if (number % 100 < 20){
soFar = numNames[number % 100];
number /= 100;
}
else {
soFar = numNames[number % 10];
number /= 10;
soFar = tensNames[number % 10] + soFar;
number /= 10;
}
if (number == 0) return soFar;
return numNames[number] + " hundred" + soFar;
}
public String convert(int number) {
/* special case */
if (number == 0) { return "zero"; }
String prefix = "";
if (number < 0) {
number = -number;
prefix = "negative";
}
String soFar = "";
int place = 0;
do {
int n = number % 1000;
if (n != 0){
String s = convertLessThanOneThousand (n);
soFar = s + majorNames[place] + soFar;
}
place++;
number /= 1000;
} while (number > 0);
return (prefix + soFar).trim();
}
public static void main(String[] args) {
EnglishDecimalFormat f = new EnglishDecimalFormat();
System.out.println("*** " + f.convert(6));
System.out.println("*** " + f.convert(9));
System.out.println("*** " + f.convert(16));
System.out.println("*** " + f.convert(100));
System.out.println("*** " + f.convert(118));
System.out.println("*** " + f.convert(200));
System.out.println("*** " + f.convert(219));
System.out.println("*** " + f.convert(800));
System.out.println("*** " + f.convert(801));
System.out.println("*** " + f.convert(1316));
System.out.println("*** " + f.convert(1000000));
System.out.println("*** " + f.convert(2000000));
System.out.println("*** " + f.convert(3000200));
System.out.println("*** " + f.convert(700000));
System.out.println("*** " + f.convert(9000000));
System.out.println("*** " + f.convert(123456789));
System.out.println("*** " + f.convert(-45));
/*
*** zero
*** one
*** sixteen
*** one hundred
*** one hundred eighteen
*** two hundred
*** two hundred nineteen
*** eight hundred
*** eight hundred one
*** one thousand three hundred sixteen
*** one million
*** two million
*** three million two hundred
*** seven hundred thousand
*** nine million
*** one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine
*** negative fourty five
*/
}
}
Thanks again Rick, DrWarezz, and CI-Ia0s
Can anybody give me any suggestions on how to implement this code that I have into each of my methods? (Thanks for the page Rick)
methods:
public class MoneyModel {
public MoneyModel() */null constructor set initial value to 0.00
public double getMoneyAmount()
public void setMoneyrAmount(double)
public String getMoneyrWords()
Public static void main(String[] args) */contain test code
code:(I know I will have to change my class names)
class EnglishDecimalFormat {
private static final String[] majorNames = {
"",
" Thousand",
" million",
" billion",
" trillion",
" quadrillion",
" quintillion"
};
private static final String[] tensNames = {
"",
" Ten",
" Twenty",
" Thirty",
" Fourty",
" Fifty",
" Sixty",
" Seventy",
" Eighty",
" Ninety"
};
private static final String[] numNames = {
"",
" One",
" Two",
" Three",
" Four",
" Five",
" Six",
" Seven",
" Eight",
" Nine",
" Ten",
" Eleven",
" Twelve",
" Thirteen",
" Fourteen",
" Fifteen",
" Sixteen",
" Seventeen",
" Eighteen",
" Nineteen"
};
private String convertLessThanOneThousand
String soFar;
if (number % 100 < 20){
soFar = numNames[number % 100];
number /= 100;
}
else {
soFar = numNames[number % 10];
number /= 10;
soFar = tensNames[number % 10] + soFar;
number /= 10;
}
if (number == 0) return soFar;
return numNames[number] + " hundred" + soFar;
}
public String convert(int number) {
/* special case */
if (number == 0) { return "zero"; }
String prefix = "";
if (number < 0) {
number = -number;
prefix = "negative";
}
String soFar = "";
int place = 0;
do {
int n = number % 1000;
if (n != 0){
String s = convertLessThanOneThousand
soFar = s + majorNames[place] + soFar;
}
place++;
number /= 1000;
} while (number > 0);
return (prefix + soFar).trim();
}
public static void main(String[] args) {
EnglishDecimalFormat f = new EnglishDecimalFormat();
System.out.println("*** " + f.convert(6));
System.out.println("*** " + f.convert(9));
System.out.println("*** " + f.convert(16));
System.out.println("*** " + f.convert(100));
System.out.println("*** " + f.convert(118));
System.out.println("*** " + f.convert(200));
System.out.println("*** " + f.convert(219));
System.out.println("*** " + f.convert(800));
System.out.println("*** " + f.convert(801));
System.out.println("*** " + f.convert(1316));
System.out.println("*** " + f.convert(1000000));
System.out.println("*** " + f.convert(2000000));
System.out.println("*** " + f.convert(3000200));
System.out.println("*** " + f.convert(700000));
System.out.println("*** " + f.convert(9000000));
System.out.println("*** " + f.convert(123456789));
System.out.println("*** " + f.convert(-45));
/*
*** zero
*** one
*** sixteen
*** one hundred
*** one hundred eighteen
*** two hundred
*** two hundred nineteen
*** eight hundred
*** eight hundred one
*** one thousand three hundred sixteen
*** one million
*** two million
*** three million two hundred
*** seven hundred thousand
*** nine million
*** one hundred twenty three million four hundred fifty six thousand seven hundred eighty nine
*** negative fourty five
*/
}
}
Thanks again Rick, DrWarezz, and CI-Ia0s
8-) ThanQ
public double getMoneyAmount() {
String s = textField.getText();
// Cast the String to a double now:
double d;
try {
d = new double( s );
} catch (Exception e) {
return null;
}
return d;
}