0

I have made a program that asks the user to enter two numbers, and it should give an error if the second number is a 0. However, I am getting an error, which is below. I have an if-else statement but it's not doing what I expect it to. I'm not sure what I'm doing wrong.

public static void main(String[] args) {
    int x, y;
    Scanner kbd = new Scanner(System.in);

    System.out.print("Enter a: ");
    x = kbd.nextInt();
    System.out.print("Enter b: ");
    y = kbd.nextInt();

    int result = add(x, y);
    int result2 = sub(x, y);
    int result3 = multi(x, y);
    int result4 = divide(x, y);
    int result5 = mod(x, y);

    System.out.println(x + " + " + y + " = " + result);
    System.out.println(x + " - " + y + " = " + result2);
    System.out.println(x + " * " + y + " = " + result3);
    System.out.println(x + " / " + y + " = " + result4);
    System.out.print(x + " % " + y + " = " + result5);
}

public static int add(int x, int y) {
    int result;
    result = x + y;
    return result;
}

public static int sub(int x, int y) {
    int result2;
    result2 = x - y;
    return result2;
}

public static int multi(int x, int y) {
    int result3;
    result3 = x * y;
    return result3;
}

public static int divide(int x, int y) {
    int result4;
    result4 = x / y;
    if (y == 0) {
        System.out.print("Error");
    } else {
        result4 = x / y; 
    }
    return result4; 
}

public static int mod(int x, int y) {
    int result5;
    result5 = x % y;
    if (y == 0) {
        System.out.print("Error");
    } else {
        result5 = x % y;
    }
    return result5;
}

Output I get this error..

Enter a: 10
Enter b: 0
Exception in thread "main" java.lang.ArithmeticException: / by zero
Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
Frank
  • 137
  • 9

3 Answers3

3

You get this because when you divide by 0, Java throws an Exception. If you just want to use an if statement to handle it, then use something like this:

public static int divide(int x, int y){
   int result;
   if ( y == 0 ) {
 // handle your Exception here
 } else {
   result = x/y; 
  }
  return result; 
}

Java also handles Exceptions through try/catch blocks, which runs code in the try block and will handle how the exceptions are processed in the catch block. So you could do:

try {  
       result4 = divide(a, b);
}
catch(//the exception types you want to catch ){
     // how you choose to handle it
}
Caleb An
  • 366
  • 1
  • 10
  • see i did have that also but erased it , because in the "//handle exception here" i tried putting system.out.print("error") but i get an int error, so im not sure what exception i could put in there – Frank Jul 15 '15 at 04:21
  • thanks caleb, for the try/catch blocks... although i have not read about them yet so i want to stick to the basic at the moment – Frank Jul 15 '15 at 04:22
  • basically you have to choose what happens if someone tries to divide by zero. In Java, the try / catch will let the rest of your code run even if the divide has an error. – Caleb An Jul 15 '15 at 04:24
  • yeah my mistake, i changed it but still an error.. not sure what im doing wrong – Frank Jul 15 '15 at 04:41
0

Ok, I copied-pasted your code verbatim, surrounded it in a class, imported java.util.Scanner and run javac. For what I can see, you have two extras "}" at the end of your file. You also have other problems: result4 and result5 are not initialised and the compiler will go mad with you, because if y == 0 is true then the return values of divide and mod methods are undefined.

nigonzalezm
  • 190
  • 2
  • 10
  • got it thank you, i changed my code, but now i get the error above – Frank Jul 15 '15 at 05:40
  • 2
    you are executing ``result4 = x/y`` before you check ``y == 0`` of course it will raise an exception. Just init ``result4`` to some integer (maybe ``result4 = 0``) – nigonzalezm Jul 15 '15 at 05:42
0
  public static void main(String[] args) {

   int x,y;
   Scanner kbd = new Scanner(System.in);

   System.out.print("Enter a: ");
   x = kbd.nextInt();
   System.out.print("Enter b: ");
   y = kbd.nextInt();


   int result = add(x,y);
   int result2 = sub(x,y);
   int result3 = multi(x,y);
   int result4 = divide(x,y);
   int result5 = mod(x,y);

   System.out.println(x +" + "+ y +" = "+ result);
   System.out.println(x +" - "+ y +" = "+ result2);
   System.out.println(x +" * "+ y +" = "+ result3);
   System.out.println(x +" / "+ y +" = "+ result4);
   System.out.print(x +" % "+ y +" = "+ result5);

   }

  public static int add(int x, int y){
   int result;
   result = x+y;
     return result;
     }


  public static int sub(int x, int y){
   int result2;
   result2 = x-y;
     return result2;
     }

  public static int multi(int x, int y){
       int result3;
       result3 = x * y;
          return result3;
     }

   public static int divide(int x, int y){
       int result4;
       result4 = 0;
        if ( y == 0 ) {
        System.out.print("Error");
          } 
        else{
          result4 = x/y; 
          }
         return result4; 
     }

   public static int mod(int x, int y){
      int result5;
      result5 = 0;
         if ( y == 0) {
            System.out.println("Error!");
          }
          else{
            result5 = x % y;
            }
            return result5;

}
}      

output

Enter a: 4
Enter b: 0
ErrorError!
4 + 0 = 4
4 - 0 = 4
4 * 0 = 0
4 / 0 = 0
4 % 0 = 0
Frank
  • 137
  • 9