-2

here is my sample code, logcat is showing exception in while statement, I am out of mind now. I don't know how and why it is being divided by zero. Help me please :

public void divisionQuestion() {
    int a;
    int b;
    Random random = new Random();
    do {
        a = random.nextInt(40);
        b = random.nextInt(20);
    } while (a % b != 0 || a < 3 || b < 2);
}

My logcat :

java.lang.IllegalStateException: Could not execute method for android:onClick
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
    at android.view.View.performClick(View.java:6256)
    at android.view.View$PerformClick.run(View.java:24701)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
    at android.view.View.performClick(View.java:6256) 
    at android.view.View$PerformClick.run(View.java:24701) 
    at android.os.Handler.handleCallback(Handler.java:789) 
    at android.os.Handler.dispatchMessage(Handler.java:98) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
 Caused by: java.lang.ArithmeticException: divide by zero
    at com.universeCoding.mathtestapp.QuestionAnswerAdapter.divisionQuestion(Adapter.java:228)
gagangamer
  • 14
  • 1
  • 6
  • You restricted `a % b != 0`, it's not the same as `b != 0`. `a % b` perform `a / b` and `b` can be 0. – Guy Dec 01 '19 at 13:20
  • 1
    `a % b` is (kind of) division, so must check if `b` is not zero - just put that at the end of the whole expression (that is, check `b < 2` before) ((or just add `1` to `b` and remove `1` from the range in `nextInt`)) – user85421 Dec 01 '19 at 13:20
  • @user85421 i already solved this problem by adding +1 to both a & b like this :(a = random.nextInt(40) +1); ( b = random.nextInt(20) + 1;) . and it's working fine now. But the thing i want to know is why these checks in while statement are not working :(a < 3 || b < 2). As i already restricted a or b from being zero(0). Correct me if i am wrong anywhere. Sorry, for bad english. – gagangamer Dec 02 '19 at 12:11
  • they are not working because you are doing `a % b` before - and if `b` is zero that will fail (this was already commented twice, and answered twice) - in expression `a % b != 0 || a < 3 || b < 2`, `a % b` will be executed very first – user85421 Dec 02 '19 at 13:47
  • oh, my mistake. RIP my english :( – gagangamer Dec 04 '19 at 12:03

2 Answers2

0

This statement:

a % b != 0

will give an error when b is zero.

The problem is that it can be zero, since this line:

b = random.nextInt(20);

will allow b to be any random value in the range [0, 20[.

ruohola
  • 21,987
  • 6
  • 62
  • 97
0

you can avoid from this problem by

adding "if(b!=0)" statement

hossein
  • 490
  • 5
  • 7
  • 3
    *add try/catch block* NO! Never use `try catch` when you can avoid the exception altogether. – Guy Dec 01 '19 at 13:24