Solved

STRINGS AND NUMBERS

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

759 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

18 Experts available now in Live!

Get 1:1 Help Now