1 Replies - 3249 Views - Last Post: 11 June 2012 - 07:38 PM Rate Topic: -----

#1 JavaSuperNoob  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 57
  • 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 :D

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  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • 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;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1