0

I am doing a GUI check writer program, I want to ask how do you use the number that the user type, and convert it into words in a JLabel?

For example: if I type 1435.56 the output would be one thousand four hundred thirty five dollars fifty six cents

Code:

 package CheckWriter;

 import java.awt.*;

  import java.awt.event.*;

  import java.text.DecimalFormat;



 import javax.swing.*;



  public class CheckPanel extends JPanel{



private JLabel amountLabel, nameLabel, orderpayLabel,bankLabel,fmtAmountLabel;

private JTextField name;

String appli_Name;

String bank_amount;

private JTextField amount;



public CheckPanel(){



    setLayout (new FlowLayout());





    nameLabel = new JLabel("Name:");

    nameLabel.setLocation(10,10);;

    add(nameLabel);



    name = new JTextField(7);

    name.setLocation(20,10);

    add(name);



    amountLabel = new JLabel("Check Amount:");

    amountLabel.setLocation(30,10);

    add(amountLabel);



    amount = new JTextField(7);

    amount.setLocation(40,10);

    add(amount);



    orderpayLabel = new JLabel("");

    orderpayLabel.setLocation(15, 30);

    add(orderpayLabel);



    bankLabel = new JLabel("    Frost Banking" + "\n");

    bankLabel.setLocation(45,25);

    add(bankLabel);



    fmtAmountLabel = new JLabel("");

    fmtAmountLabel.setLocation(45, 35);

    add(fmtAmountLabel);



    event e = new event();

           name.addActionListener(e);

           amount.addActionListener(e);

  }

   private class event implements ActionListener{

    public void actionPerformed(ActionEvent e) {



        appli_Name = name.getText();

        orderpayLabel.setText("Pay to the order of :" +" "   + appli_Name);



        bank_amount = amount.getText();

        double amount = Double.parseDouble(bank_amount);

        DecimalFormat formatter = new DecimalFormat("$###,###,###,###.##");

        fmtAmountLabel.setText(formatter.format(amount));

    }

}
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
Noone
  • 7
  • 2

2 Answers2

0

For your problem with the amount: You have to split up the String with amount.getText().split(".");

Then you can parse the two strings with the following class (See related question: How to convert number to words in java) with the following class (copied from the refered question):

import java.text.DecimalFormat;

public class EnglishNumberToWords {

    private static final String[] tensNames = {
        "",
        " ten",
        " twenty",
        " thirty",
        " forty",
        " 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 EnglishNumberToWords() {}

    private static 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 static String convert(long number) {
         // 0 to 999 999 999 999
        if (number == 0) { return "zero"; }

        String snumber = Long.toString(number);

        // pad with "0"
        String mask = "000000000000";
        DecimalFormat df = new DecimalFormat(mask);
        snumber = df.format(number);

       // XXXnnnnnnnnn
       int billions = Integer.parseInt(snumber.substring(0,3));
       // nnnXXXnnnnnn
       int millions  = Integer.parseInt(snumber.substring(3,6));
       // nnnnnnXXXnnn
       int hundredThousands = Integer.parseInt(snumber.substring(6,9));
       // nnnnnnnnnXXX
       int thousands = Integer.parseInt(snumber.substring(9,12));

       String tradBillions;
       switch (billions) {
           case 0:
               tradBillions = "";
               break;
           case 1 :
               tradBillions = convertLessThanOneThousand(billions)+ " billion ";
               break;
           default :
  tradBillions = convertLessThanOneThousand(billions)
  + " billion ";
}
String result =  tradBillions;

String tradMillions;
switch (millions) {
case 0:
  tradMillions = "";
  break;
case 1 :
  tradMillions = convertLessThanOneThousand(millions)
     + " million ";
  break;
default :
  tradMillions = convertLessThanOneThousand(millions)
     + " million ";
}
result =  result + tradMillions;

String tradHundredThousands;
switch (hundredThousands) {
case 0:
  tradHundredThousands = "";
  break;
case 1 :
  tradHundredThousands = "one thousand ";
  break;
default :
  tradHundredThousands = convertLessThanOneThousand(hundredThousands)
     + " thousand ";
}
result =  result + tradHundredThousands;

String tradThousand;
tradThousand = convertLessThanOneThousand(thousands);
result =  result + tradThousand;

// remove extra spaces!
return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");

}

This class can convert your integers into the correct format:

String yourstring = EnglishNumberToWords.convert(800));

Then just display this String. Note: It only work with numbers between 0 and 999 999 999 999

qry
  • 457
  • 3
  • 11
0

Try Me.Cursor = System.Windows.Forms.Cursors.WaitCursor DbConnect()

        Dim mySelectQuery As String = "Select * From tblCheckBank where BankName = '" & Trim(Replace(Trim(cboBank.Text), "'", "''")) & "' And CompID = " & CurrCompanyID
        Dim myCommand As New SqlClient.SqlCommand(mySelectQuery, cnn)
        myCommand.CommandType = CommandType.Text
        Dim myReader As SqlClient.SqlDataReader
        myReader = myCommand.ExecuteReader()

        While myReader.Read
            CheckBankName = myReader("BankName")
            CheckTotalWidth = myReader("BankX")
            CheckTotalHeight = myReader("BankY")
            CheckBearerHeight = myReader("BankA")
            CheckBearerWidth = myReader("BankB")
            CheckDateHeight = myReader("BankC")
            CheckDateWidth = myReader("BankD")
            CheckAmtHeight = myReader("BankE")
            CheckAmtWidth = myReader("BankF")
            CheckAmtWordHeight = myReader("BankG")
            CheckAmtWordWidth = myReader("BankH")
            CheckLine1Width = myReader("BankI")
            CheckAmtWordHeightLine2 = myReader("BankJ")
            CheckAmtWordWidthLine2 = myReader("BankK")
            CheckAccPayeeHeight = myReader("BankM")
            CheckAccpayeeWidth = myReader("BankN")
            CheckNegoHeight = myReader("BankO")
            CheckNegoWidth = myReader("BankP")
            CheckOverHeight = myReader("BankQ")
            CheckOverWidth = myReader("BankR")



            CheckTotalWidth = CheckTotalWidth * 100
            CheckTotalHeight = CheckTotalHeight * 100

            CheckBearerWidth = (CheckBearerWidth * 100) - 32
            CheckBearerHeight = (CheckBearerHeight * 100) + (950 - CheckTotalHeight) - 32

            CheckDateWidth = (CheckDateWidth * 100) - 32
            CheckDateHeight = (CheckDateHeight * 100) + (950 - CheckTotalHeight) - 32

            CheckAmtWidth = (CheckAmtWidth * 100) - 32
            CheckAmtHeight = (CheckAmtHeight * 100) + (950 - CheckTotalHeight) - 32

            CheckAmtWordWidth = (CheckAmtWordWidth * 100) - 32
            CheckAmtWordHeight = (CheckAmtWordHeight * 100) + (950 - CheckTotalHeight) - 32

            CheckAmtWordWidthLine2 = (CheckAmtWordWidthLine2 * 100) - 32
            CheckAmtWordHeightLine2 = (CheckAmtWordHeightLine2 * 100) + (950 - CheckTotalHeight) - 32

            CheckLine1Width = CheckLine1Width * 100

            CheckAccpayeeWidth = (CheckAccpayeeWidth * 100) - 32
            CheckAccPayeeHeight = (CheckAccPayeeHeight * 100) + (950 - CheckTotalHeight) - 32

            CheckNegoWidth = (CheckNegoWidth * 100) - 32
            CheckNegoHeight = (CheckNegoHeight * 100) + (950 - CheckTotalHeight) - 32

            CheckOverWidth = (CheckOverWidth * 100) - 32
            CheckOverHeight = (CheckOverHeight * 100) + (950 - CheckTotalHeight) - 32

        End While

        myReader.Close()
        DbClose()
        Me.Cursor = System.Windows.Forms.Cursors.Arrow

    Catch ex As Exception
        SendErrorMail(ex.ToString, CurrCompanyName)
        Exit Sub
    End Try
  • 1
    Code-only answers are generally frowned upon on this site. Could you please edit your answer to include some comments or explanation of your code? Explanations should answer questions like: What does it do? How does it do it? Where does it go? How does it solve OP's problem? See: [How to anwser](https://stackoverflow.com/help/how-to-answer). Thanks! – Eduardo Baitello Sep 19 '19 at 20:32