Solved

STRINGS AND NUMBERS

Posted on 1998-07-06
1
227 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 80
Should localization be done inside spring controller 5 26
use lov values 2 50
Java Restore security prompts not working 10 12
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

772 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