# Thread: How do I prevent division by zero in my code?

1. Senior Member
Join Date
Apr 2012
Posts
115
Rep Power
0

## How do I prevent division by zero in my code?

I have one problem with my code, when I choose division it sometimes gives a division by zero error. how do I avoid this? as you can see I tried an if statement to catch that possibility but I still get division by zero errors.

Java Code:
```package chapter_vi;

import java.util.Random;
import java.util.Scanner;

public class ComputerAssistedInstructionVaryingTheTypesOfProblems_6_39
{
static Scanner input = new Scanner(System.in);

public static int quiz(int difficulty, int problemType)
{
int totalCorrect = 0;
int totalIncorrect = 0;

for( int i = 1 ; i <= 10 ; i++ )
{
boolean Correct = question(difficulty, problemType);

if (Correct == true)
{
totalCorrect++;
}
else
{
totalIncorrect++;
}

}

if (totalCorrect >= ((double)10/100) * 75)
{
System.out.println("Congratulations, you are ready to go to the next level!");
difficulty++;

System.out.println("total is " + totalCorrect + " total incorrect is " + totalIncorrect);
System.out.println(((double)10/100)*75);

problemType = getProblemType();
quiz(difficulty, problemType);
}
else
{

problemType = getProblemType();
question(difficulty, problemType);
}

}

public static boolean question(int difficulty, int problemType)
{
Random randomNumbers = new Random();
boolean correct = false;

int random1 = 0;
int random2 = 0;
int result = 0;

if( difficulty == 1)
{
random1 = randomNumbers.nextInt(11);
random2 = randomNumbers.nextInt(11);
}
else if( difficulty == 2)
{
random1 = randomNumbers.nextInt(101);
random2 = randomNumbers.nextInt(101);
}
else // difficulty 3 (>= 3)
{
random1 = randomNumbers.nextInt(1001);
random2 = randomNumbers.nextInt(1001);
}

switch(problemType)
{
case 1:
result = (random1 + random2);
System.out.println("What is " + random1 + " + " + random2 + "?");
break;
case 2:
result = (random1 - random2);
System.out.println("What is " + random1 + " - " + random2 + "?");
break;
case 3:
result = (random1 * random2);
System.out.println("What is " + random1 + " \u00d7 " + random2 + "?");
break;
case 4:
if( random1 == 0 || random2 == 0 )
{
question(difficulty, problemType);
}
result = (random1 / random2);
System.out.println("What is " + random1 + " \u00f7 " + random2 + "?");
break;
case 5:
problemType = 1 + randomNumbers.nextInt(4);
question(difficulty, problemType);
}

{
switch(1 + randomNumbers.nextInt(4))
{
case 1:
System.out.println("Very good!");
break;
case 2:
System.out.println("Excellent!");
break;
case 3:
System.out.println("Nice work");
break;
case 4:
System.out.println("Keep up the good work");
}

correct = true;
}
else
{
switch(1 + randomNumbers.nextInt(4))
{
case 1:
break;
case 2:
System.out.println("Wrong. Try once more.");
break;
case 3:
System.out.println("Don't give up!");
break;
case 4:
System.out.println("No. Keep trying.");
}

correct = false;
}

return correct;
}

public static int getDifficulty()
{
int difficulty;
System.out.println("What level of difficulty would you like? (1, 2, 3)");
difficulty = input.nextInt();

return difficulty;
}

public static int getProblemType()
{
int problemType;
System.out.println("What type of problem would you like to solve? (1 = addition," +
"2 = subtraction, 3 = multiplication, 4 = division, 5 = all)");
problemType = input.nextInt();

return problemType;
}

public static void main(String[] args)
{
int difficulty;
int problemType;

difficulty = getDifficulty();
problemType = getProblemType();

quiz(difficulty, problemType);
}
}```
Whats going on and how do I fix it guys?

thank you

2. ## Re: How do I prevent division by zero in my code?

Please post the full text of the error message that shows where the error is.

3. Senior Member
Join Date
Feb 2012
Posts
117
Rep Power
0

## Re: How do I prevent division by zero in my code?

Java Code:
```if( random1 == 0 || random2 == 0 )
{
question(difficulty, problemType);
}
result = (random1 / random2);```
So after you check to see what if something is zero, what does your code do? Specifically, does your code actually change random1 or 2? Remember, variables declared within a method belong to that method - if you call a method again, even inside that method, new variables are made - the old ones aren't overwritten.

4. ## Re: How do I prevent division by zero in my code?

"try...catch"? btw, what diargg said is a hint...

5. Senior Member
Join Date
Apr 2012
Posts
115
Rep Power
0

## Re: How do I prevent division by zero in my code?

I'm so sorry I'm just not good with this stuff I'm trying to understand but I don't. I'll just think loudly for you guys. Below are my thoughts which I'll write as I'm trying to work it out, I usually get things wrong atleast this way you guys might know why I get it wrong.

what does diargg mean? well hes talking about local variables... hes saying that when question is called again new values are created. what would be the values of these? they would be a pass-by-value copy... so question() must get a copy of the current values I'm dealing with.

ok so I'll look at the start of my method since I called it. since difficulty is the same, I must get the same difficulty like last time but then random1+2 should get new values however I think diargg is saying that this copy of random1+2 dont affect the original random1+2 but why does that matter? does program control ever return to the original call of the method? well... lets say my new randoms give me a good result then what happens...

wait a minute I must return back to the original method call! and this will execute random1/random2 with their original values!!! thats it... is it.

yay I might have worked it out!

ok so whats the solution... well I dont want to continue the original cycle I want to go back to the beginning and as I've discovered I cant do that with a method call like that... how do I return program control to the beginning? I would put break afterwards but then it will still check the value of input against result later in the code and that will result in a wrong answer so I cant use break.

I know! GoTo!!! *smack* sorry... (speaking of which does java even have that?) um... agh!I just want to get back to the beginning of the method, why is that so hard...

also I notice that if I select "all" I get this strange result

What level of difficulty would you like? (1, 2, 3)
1
What type of problem would you like to solve? (1 = addition,2 = subtraction, 3 = multiplication, 4 = division, 5 = all)
5
What is 5 + 9?
3
Don't give up!
14
Wrong. Try once more.
What is 0 - 5?
-5
Very good!
4
What is 2 - 3?
-1
Keep up the good work
3
Don't give up!
What is 2 + 10?

this is probably also related to the method call problem isnt it?, I bet it asks the question again in the new method call but then it returns and thats why you input twice...

how do I return control to the beginning guys?

6. Senior Member
Join Date
Jun 2007
Location
Bali, Indonesia
Posts
762
Rep Power
14

## Re: How do I prevent division by zero in my code?

Basically to prevent divide by zero error your code have to guard for this possibility. You should make sure that before you are doing any integer division the divider doesn't not equals to zero.

7. Senior Member
Join Date
Apr 2012
Posts
115
Rep Power
0

## Re: How do I prevent division by zero in my code?

I fixed the code by saying
correct = question(difficulty,problemType)
return correct;

but I learned something, and you've just said it... only the divider mustnt be zero, I suppose that makes sense 0 shared between 13 would be 0.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•