Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

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!

0
DreamWeaver777
Asked:
DreamWeaver777
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
Rick_TownsendCommented:
I don't think I've seen this question before.  See here for a good example:

http://www.rgagnon.com/javadetails/java-0426.html
0
 
DrWarezzCommented:
Firstly, to retrieve the number inputted into the text field, and assign it to a double, you simply do this:

public double getMoneyAmount() {
   String s = textField.getText();
   // Cast the String to a double now:
   try {
      double d = new double( s );
   } catch (Exception e) {
      return null;
   }
   return d;
}

You'd then use this to call the getMoneyAmount() method:

   double amount = getMoneyAmount();
   if ( amount == null ) {
      // An error occured.
      // The inputted value is probably not of type double.
   }

>"...should come out as Seven Thousand Eight Hundred Fifty Six Dollars and 89 Cents"
I don't know of a class that does this for you, you'd probably need to code you own algorithm to do this. :(

give me a sec, and i'll look over everything else a bit more...

gL,
[r.D]
0
 
DrWarezzCommented:
Woops, change that method to:

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;
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Rick_TownsendCommented:
dollars and cents are at the bottom
0
 
DreamWeaver777Author Commented:
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(EXIT_ON_CLOSE);
    }

    //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(false);
    outputArea.setLineWrap(true);
    outputArea.setWrapStyleWord(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,BorderLayout.NORTH);
    this.getContentPane().add(centerPanel,BorderLayout.CENTER);
    this.getContentPane().add(goButton,BorderLayout.SOUTH);
  }

  public void actionPerformed (ActionEvent e) {
    try {
      String inText = inputArea.getText();
      double inValue = Double.parseDouble(inText);
      theModel.setCheckAmount(inValue);
      outputArea.setText(theModel.getMoneyWords());
    }
    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();
  }
}
0
 
DrWarezzCommented:
: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]
0
 
CI-Ia0sCommented:
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!
0
 
Rick_TownsendCommented:
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."
0
 
DreamWeaver777Author Commented:
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
0
 
DrWarezzCommented:
8-) ThanQ
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now