Solved

STRINGS AND NUMBERS

Posted on 1998-07-06
1
236 Views
Last Modified: 2010-03-30
I am producing a simple Applet where the user enters a number into a text field and the computer performs some basic mathematics on the number entered. The problem is that the only commands that I can find for getting ands setting the field are 'getText' and 'setText'. These two commands only work on strimng value and I cannot find any Java commands for switching between strings and numbers.

Any help would be much appreiated.

Richard H
0
Comment
Question by:RICHARDH
1 Comment
 
LVL 6

Accepted Solution

by:
gadio earned 30 total points
ID: 1224713
RICHARDH, what you need is to use the formatters that java provides. The formatters are part of the java.text package. Here is an samll example of how to use diferent types of them:
---------
    //.....
    //Formatters to format and parse numbers
    private NumberFormat moneyFormatter;
    private NumberFormat percentFormatter;
    private NumberFormat integerFormatter;
    private DateFormat dateFormatter;

    //.....
    //.....
    private void setUpFormatters() {
        dateFormatter = new SimpleDateFormat("dd/MM/yy HH:mm");

        moneyFormatter = NumberFormat.getCurrencyInstance(Locale.US);

        percentFormatter = NumberFormat.getPercentInstance(Locale.US);
        percentFormatter.setMinimumFractionDigits(3);

        integerFormatter = NumberFormat.getNumberInstance(Locale.US);
        integerFormatter.setParseIntegerOnly(true);
    }
    //.....
    //.....
    //.....
   
        // using a formatter inside some function:
        // theformatter is one of the formatters instances defined above.
        public void actionPerformed(ActionEvent evt) {

            JTextField inputField = (JTextField)evt.getSource();

            try {
                if( theformatter instanceof NumberFormat ) {
                    NumberFormat formatter = (NumberFormat)theformatter;
                    Number value = formatter.parse(inputField.getText());
                    if (inputField == amountField)
                        amount = value.doubleValue();
                    else if (inputField == rateField)
                        rate = value.doubleValue();
                    else if (inputField == numPeriodsField)
                        numPeriods = value.intValue();
   
                    //Recalculate
                    payment = computePayment(amount, rate, numPeriods);

                    // Display
                    paymentField.setText(moneyFormatter.format(payment));
                }
                else {
                    DateFormat formatter = (DateFormat)theformatter;
                    Date value = formatter.parse(inputField.getText());

                    // Display
                    paymentField.setText(dateFormatter.format(value));
                }
            } catch (ParseException e) {
                Toolkit.getDefaultToolkit().beep();
                paymentField.setText("Input Error");
            }
        }
    }
-----------------------------
Hope that will help you.
G.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
windows explorer path to command prompt 5 54
servlet example issue 6 46
Why my table column Id is not passed to java object? 4 44
Java Eclipse Loop 3 20
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question