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
			a=Integer.parseInt(temp1);	
			

	//input second number
	String temp2 = JOptionPane.showInputDialog(null,"Please another number.");
			//convert to int
			b=Integer.parseInt(temp2);		
			
			
	//input third number
	String temp3 = JOptionPane.showInputDialog(null,"Please another number.");
			//convert to int
			c=Integer.parseInt(temp3);
			
	//input fouth number
	String temp4 = JOptionPane.showInputDialog(null,"Please another number.");
			//convert to int
			d=Integer.parseInt(temp4);
			
			
	//input fifth number
	String temp5 = JOptionPane.showInputDialog(null,"Please another number.");
			//convert to int
			e=Integer.parseInt(temp5);	
			
	//input sixth number
	String temp6 = JOptionPane.showInputDialog(null,"Please another number.");
			//convert to int
			b=Integer.parseInt(temp6);
			
	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
			
			}	
		
	}

Recommended Answers

All 2 Replies

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.

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.