Hi I am trying to ride a program that answers a mathematic equation but I keep getting the the error Exception in thread "main" java.lang.ArithmeticException: / by zero
at EquationCalc.main(EquationCalc.java:48)
I cannot get around it, does anyone have any ideas?

import javax.swing.JOptionPane;

/** This program will calculate an equation following the order of operations */
public class EquationCalc
public static void main(String[] args)
	int a=0, b=0, c=0, d=0, e=0, f=0;

	JOptionPane.showMessageDialog(null, "Order of operation!");//Stating purpose of program to user
	//input first number
	String temp1 = JOptionPane.showInputDialog(null,"Please enter a number.");
			//convert to int

	//input second number
	String temp2 = JOptionPane.showInputDialog(null,"Please another number.");
			//convert to int
	//input third number
	String temp3 = JOptionPane.showInputDialog(null,"Please another number.");
			//convert to int
	//input fouth number
	String temp4 = JOptionPane.showInputDialog(null,"Please another number.");
			//convert to int
	//input fifth number
	String temp5 = JOptionPane.showInputDialog(null,"Please another number.");
			//convert to int
	//input sixth number
	String temp6 = JOptionPane.showInputDialog(null,"Please another number.");
			//convert to int
	JOptionPane.showMessageDialog(null, "We will now solve for the equation x = a + (double)b * (c / d) – e % f");
	double x= a + (double)b * (c / d) - e % f;//user dialog box stating purpose
	System.out.println( x+"="+a+"+"+(double)b+"*"+(c+"/"+d)+"-"+e+"%"+f);// prints out answer

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

8 Years
Discussion Span
Last Post by Taywin

Divide by zero means exactly what it says.
You have c/d and also %f so it seems that either d or f must be zero.
Print out all the variables immediately before the line where the problem occurs and see which one(s) are zero.
When you know which is zero, track back thru your logic to find out why.


Just want to add a little more suggestion. When dealing with division and you know that divided by 0 cannot be done (or the output is infinity which is impossible to give an exact value in computer), you should either check for '0' denominator or create an exception handling mechanism. I would suggest you to do the check for now until you gain more experience in Java.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.