Tryin to get the "Division by zero not allowed" part working ca someone help please :cry:

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.applet.Applet;

public class JDivideMe extends Applet {
    private TextField numeratorField = new TextField( 10 );
    private TextField denominatorField = new TextField( 10 );
    private Label theAnswer = new Label("your answer is");
    private Button divideButton;

    public static final String DIVIDE = "divide";

    public JDivideMe() {
        super();
    }
    
    public void doDivide() {
        double numerator = Double.parseDouble(numeratorField.getText());
        double denominator = Double.parseDouble(denominatorField.getText());
        if (denominatorField == zero) {
            System.out.println("Division by zero not allowed"); 
        } else {
            double result = numerator/denominator;
            theAnswer.setText(" = " + result );
        }
    }
    
    public void init() {
        divideButton = new Button("/");
        divideButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                doDivide();
            }
        });
        add(divideButton);
        add(numeratorField);
        add(denominatorField);
        add(theAnswer);
    }
}

Hi, there.
What is the variable 'zero' supposed to be ?
Also, 'denominatorField' is a component, why do you compare a component with the variable 'zero' (I assume zero = 0).

As red_evolve has already mentioned, there appears to be no definition for 'zero' in the code you have posted. Since you have already parsed the denominatorField (TextField) into denominator (double) why not just compair 'denominator == 0'

Hi everyone,

As red_evolve has already mentioned, there appears to be no definition for 'zero' in the code you have posted. Since you have already parsed the denominatorField (TextField) into denominator (double) why not just compair 'denominator == 0'

Yup that's the best way to do it

Richard West

Ok thanks guys for your reply i will give it a shot like that :mrgreen:

OK so now I have this code below and it will complie but the only problem is that the applet will not disply the "Division by zero not allowed" when trying to divide with 0 can anyone help me with this issue please?

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.applet.Applet;

public class JDivideMe extends Applet {
    private TextField numeratorField = new TextField( 10 );
    private TextField denominatorField = new TextField( 10 );
    private Label theAnswer = new Label("your answer is");
    private Button divideButton;

    public static final String DIVIDE = "divide";

    public JDivideMe() {
        super();
    }
    
    public void doDivide() {
        double numerator = Double.parseDouble(numeratorField.getText());
        double denominator = Double.parseDouble(denominatorField.getText());
        if (denominator == 0) {
            System.out.println("Division by zero not allowed"); 
        } else {
            double result = numerator/denominator;
            theAnswer.setText(" = " + result );
        }
    }
    
    public void init() {
        divideButton = new Button("/");
        divideButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                doDivide();
            }
        });
        add(divideButton);
        add(numeratorField);
        add(denominatorField);
        add(theAnswer);
    }
}

You're using System.out.println() with an applet...Not something you can do. You'll need to have a paint method and draw the string to the applet...Of course you could escape all of that by using JOptionPanes.

The string "Division by zero not allowed" will not be displayed on the applet, it will be displayed in the console. You can see this messege by opening the Java Console while the applet is running (useful for debugging).

Look at how you are displaying the answer to the user, and use the same approch for the 'not allowed' message. Then it will be displayed on the applet not the console.

--------------------

Edit: Oh, I didn't see your post server_crash. I'm still getting used to this site and this hybrid mode of browsing. You can System.out.println in an applet by the way.

Edit: Oh, I didn't see your post server_crash. I'm still getting used to this site and this hybrid mode of browsing. You can System.out.println in an applet by the way.

I know, but I think he wanted another way other than opening up the java console.

You're using System.out.println() with an applet...Not something you can do.

I guess that through me then

---------------

Mike, use another JTextField or JLabel to display the message, as your doing for the answer ... it is the simplest way

OK thanks Kate and Server_Crash for your help :cheesy:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.