Solved

STRINGS AND NUMBERS

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JAVA part two 5 56
why cannot we forward request once the response is committed 2 33
javap bin 2 24
windows explorer path to command prompt 5 28
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now