# Can't divide by Zero?

Page 1 of 1

## 1 Replies - 6697 Views - Last Post: 11 June 2012 - 07:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=282489&amp;s=b9fefd86f3d58f41492fbaef2e6acae2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 JavaSuperNoob

Reputation: -1
• Posts: 59
• Joined: 13-February 12

# Can't divide by Zero?

Posted 11 June 2012 - 05:26 PM

Okay this is embarrassing .... Here is my question
Read in 5 likes of Equations from a text file and then solve them equations and display their final answer *oerder of operation not required. Also there will be an x that we will have to ask the user to enter the value of Well here is my text file
```4 + 10 - 20 / 3 * x
4 + 10 - 20 / 3 * x
4 + 10 - 20 / 3 * x
4 + 10 - 20 / 3 * x
4 + 10 - 20 / 3 * x
```

Here is my code and it tells me i can't divide by zero...
```import java.util.*;
import java.io.*;

public class SS24 {

public static void main(String[] args) throws IOException {
double dAns;
equation arCequations[] = new equation[5];
String sLine;
Scanner sin = new Scanner(new FileReader("Eq.txt"));
for (int i = 0; i < 5; i++) {
arCequations[i] = new equation();
sLine = sin.nextLine();
dAns = arCequations[i].populate(sLine);
System.out.println(dAns);
}
}
}

class equation {

Scanner sin = new Scanner(System.in);
String sChar, sLine;
int nString = 0, nX, nInt = 0;
boolean bCheck = false;
int[] arnNumbers = new int[6];
char[] arcStrings = new char[4];
double dAns;
char arcTest[] = {'+', '-', '/', '*', 'x'};

double populate(String _sLine) {
sLine = _sLine;
for (int i = 0; i < sLine.length(); i++) {
for (int j = 0; j < arcTest.length; j++) {
if (sLine.charAt(i) == arcTest[j]) {
if (sLine.charAt(i) == 'x') {
System.out.println("Enter value of X");
nX = sin.nextInt();
arnNumbers[nInt] = nX;
bCheck = true;
nInt++;
} else {
arcStrings[nString] = sLine.charAt(i);
bCheck = true;
nString++;
}
}
}
if (sLine.charAt(i) != ' ' && !bCheck) {
System.out.println(nInt);
arnNumbers[nInt] += (int) sLine.charAt(i);
System.out.println(arnNumbers[nInt]);
nInt++;
}
}

dAns = calculate(arnNumbers, arcStrings);
return dAns;
}

double calculate(int arnNumbers[], char[] arcOperators) {
double dFinal = 0;
for (int i = 0; i < arnNumbers.length - 2; i++) {
if (arcOperators[i] == '+') {
dFinal = arnNumbers[i] + arnNumbers[i + 1];
}
if (arcOperators[i] == '-') {
dFinal = arnNumbers[i] - arnNumbers[i + 1];
}
if (arcOperators[i] == '*') {
dFinal = arnNumbers[i] * arnNumbers[i + 1];
}
if (arcOperators[i] == '/') {
System.out.println(arnNumbers[i]+" "+arnNumbers[i+3]);
dFinal = arnNumbers[i] / arnNumbers[i + 1];
}
}
return dFinal;
}
}
```

Stack trace
```Exception in thread "main" java.lang.ArithmeticException: / by zero
at equation.calculate(SS24.java:76)
at equation.populate(SS24.java:58)
at SS24.main(SS24.java:15)
Java Result: 1
```

for some reason the integer that gets accumulated to the array is not correct ....
help plox

This post has been edited by JavaSuperNoob: 11 June 2012 - 05:31 PM

Is This A Good Question/Topic? 0

## Replies To: Can't divide by Zero?

### #2 cassiopeia

Reputation: 10
• Posts: 86
• Joined: 03-April 11

## Re: Can't divide by Zero?

Posted 11 June 2012 - 07:38 PM

a quick looking at error message i think (CMIIW) that happen because some thing like this : 0/1.

you can use try catch to avoid devide by zero error.

```try{
dFinal = arnNumbers[i] / arnNumbers[i + 1];
}
catch(ArithmeticException e){
dFinal = 0;
}
```