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);
    }
}

Dani AI

Generated

A few concise, practical points to close the gap in the thread and make the applet behave reliably.

The reason the "Division by zero not allowed" text never appeared is that it was sent to the Java console (via println) rather than the applet UI. As and noted, console output is useful for debugging but won't appear on the applet itself for most users. Also note a Java detail: dividing integers by zero throws an ArithmeticException, but dividing floating-point values produces Infinity or NaN — so it’s important to check for zero (or check the result with Double.isInfinite/Double.isNaN) when using doubles.

A robust approach (no fragile assumptions): validate input first, then parse, then guard the division. Wrap parsing in a try/catch for NumberFormatException and display a clear message in the same UI element used for the result (rather than printing to System.out). If the parsed denominator equals 0.0 then set the label to the "division by zero" message; otherwise compute and format the result. Trim input or disable the divide control until both fields contain valid numeric text to avoid repeated parse errors.

A couple of extra tips: if sticking with AWT, keep feedback in an AWT Label/TextField rather than mixing heavy-weight AWT controls with Swing dialogs (JOptionPane can work, but mixing toolkits can cause z‑order or look‑and‑feel quirks). For quick debugging on older setups, the Java Console or appletviewer is useful. This summary builds on suggestions from , , and and fills in input‑validation and floating‑point behavior that were not shown in the original replies.

Recommended Answers

All 10 Replies

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.