Solved

STRINGS AND NUMBERS

Posted on 1998-07-06
1
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

763 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