Java Tutorial - lesson nine


Lesson Nine: Reading values from the text fields; calculating and displaying the result.

previous lesson - back to tutorial index - next lesson


Okay. So now the buttons work. It is time to read the values in the text fields, to perform the arithmic operation and to display the result in the Label in Calculator. This will be a long lesson with some new classes, methods and concepts, so take your time.

Add the next three methods in Calculator:

//---------------------------------------------
    public double getFirstValue() {
        try {
            String str;
            Double d;
            double r;
            str= numberOne.getText();
            d= new Double(str);
            r= d.doubleValue();
            return r;
        }
        catch(Exception e) {
            System.out.println("Parsing error...");
        }
        return 0;
    }
//---------------------------------------------
    public double getSecondValue() {
        try {
            return (new Double(numberTwo.getText())).doubleValue();
        }
        catch(Exception e) {
            System.out.println("Parsing error...");
        }
        return 0;
    }
//---------------------------------------------
    public void setResult(double r) {
        result.setText("Answer: "+r);
    }

 

The first two methods read the value in the text fields. The first version is elaborate, and the second version is much more compact. I will discuss the code and the new classes further when reviewing the code. The third method is quite easy to understand: ´result´ is a Label, and a Label has a method ´setText(String s)´ to change the text to displays.

Change the method ´actionPerformed(ActionEvent e)´ of ButtonListener into:

    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==calculator.buttonAdd) {
            System.out.println("Button + was pressed...");
            double a;
            double b;
            a= calculator.getFirstValue();
            b= calculator.getSecondValue();
            calculator.setResult(a+b);
        }
        if(e.getSource()==calculator.buttonSub) {
            System.out.println("Button - was pressed...");
            double a, b;
            a= calculator.getFirstValue();
            b= calculator.getSecondValue();
            calculator.setResult(a-b);
        }
        if(e.getSource()==calculator.buttonMul) {
            System.out.println("Button * was pressed...");
            double a;
            a= calculator.getFirstValue()*calculator.getSecondValue();
            calculator.setResult(a);
        }
        if(e.getSource()==calculator.buttonDiv) {
            System.out.println("Button / was pressed...");
            double a= calculator.getFirstValue()/calculator.getSecondValue();
            calculator.setResult(a);
        }
    }

 

The idea is clear, I think. When you press a button, the two values are read from the text fields in calculator, and the desired operation is performed on them. You send the result of this operation back to ´calculator´. The code for the four operations is not equal. You might want to have a better look to get a feeling how you can condense your code and combine declarations and calculations.

Compile and run the code. You´ll see that our calculator is ready for action.

Reviewing the code:

Right. The difficult part is in understanding how we take the numbers from the text fields. In a text field, you enter text. In programming languages, text is not the same as a number. And we need numbers to calculate numbers. Text can represent a number, though, and the trick is this conversion. Take a look at the class Double: http://java.sun.com/j2se/1.3/docs/api/index.html . Bookmark this page or download it from the sun site and put it on your local disk! You can construct a Double with a String (a chain of characters, or text if you wish). The class Double will take care of parsing (translating) the text into a number. But to retrieve the value from the ´Double´ to calculate with it, we have to take the ´double´ value from the Double class. A ´double´ is a double precision number. We take this double value with the method ´doubleValue()´ in the class Double. Yes, I agree, not very direct.

Then there is one more thing: Parsing numbers does not always work. If by accident you type a letter in the text field, Double(String) cannot make a valid number of the String. Java will protest when you run the program, so you have to ´catch´ this error. Java won´t even compile if you do not catch this error in your code.

    try {
        ...
    }
    catch(Exception e) {
        ...
    }

 

Will do the catching job for you. Place the vulnerable code, prone to an error in the ´try´ statement and the error handling code in the catch part. In Calculator, if an error comes up when retrieving the number, the execution will ´fall through´ the code in the catch statement, and then execute the error handling. This error handling displays a message is your DOS/UNIX window and returns a default value of 0 because we will have to return something (the return type of the method is ´double´ and not ´void´).

previous lesson - back to tutorial index - next lesson