Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Dividing by Zero Issue ( Project - Cant figure out how to do it, or why mine is not working )

1. ## Dividing by Zero Issue ( Project - Cant figure out how to do it, or why mine is not working )

[ SOLVED ]

So, I'm trying to have the program display " You can not divide by zero " When the user divides by zero. ive read my book that i have ( purchased from school) - And i cant figure it out.

Once the user Enters " 4 " as the menuNumber - It goes into the calculator as being a division problem.

Im not really sure how to approach this, thus why i need some help.
I will be posting the full code below. Im not sure if i shouldnt, but i really cant figure this out D:
```" import java.util.Scanner;

public class BasicCalculator {
public static void main(String[] args) {
//Create Scanner
Scanner input = new Scanner(System.in);

System.out.println("2. Sub ");
System.out.println("3. Multiply ");
System.out.println("4. Divide ");
System.out.println("5. Generate a Random Number");

// Complete the input on what you would like to do.
System.out.println(" What would you like to do?");
if (menuNumber < 1 ) {
System.out.println("Im Sorry, You must choose Between 1 and 5.");
}
if (menuNumber > 5 ) {
System.out.println("Im Sorry, You must choose Between 1 and 5.");
}
System.out.println("What is the first Number?:");
int number1 = input.nextInt();
System.out.println("What is the Second Number?:");
int number2 = input.nextInt();
int answer = number1 + number2;
}
System.out.println("What is the first Number?:");
int number1 = input.nextInt();
System.out.println("What is the Second Number?:");
int number2 = input.nextInt();
int answer = number1 - number2;
}
System.out.println("What is the first Number?:");
int number1 = input.nextInt();
System.out.println("What is the Second Number?:");
int number2 = input.nextInt();
int answer = number1 * number2;
}
System.out.println("What is the first Number?:");
int number1 = input.nextInt();
System.out.println("What is the Second Number?:");
int number2 = input.nextInt();
System.out.print("Answer : " + number1 / number2);

if  (number2 <= 0) {
System.out.println("You can Not divide by zero.");
}
}
System.out.println("What is the lower limit?:");
int min = input.nextInt();
System.out.println("What is the upper limit?:");
int max = input.nextInt();

int answer = (int) (Math.ceil(Math.random() * (max - min)) + min);

}
}
}
"```
Now everything works- I just cant figure out the diving by zero issue.
If you have a link to a page that will help me, please do. I would like to understand what i need to do.

2. ## Re: Dividing by Zero Issue ( Project - Cant figure out how to do it, or why mine is not working )

Welcome to the forum! If you haven't already, please read this topic to learn other useful info for new members. I see you've partially worked out code tags. Please post all code - greater than a line or two - in code tags.
Now everything works- I just cant figure out the diving by zero issue.
What "issue"? How to detect when the user attempts to divide by zero? What to do about it? It's not clear what you need help with.

3. ## The Following User Says Thank You to GregBrannon For This Useful Post:

Famous112 (September 6th, 2014)

4. ## Re: Dividing by Zero Issue ( Project - Cant figure out how to do it, or why mine is not working )

Originally Posted by GregBrannon
Welcome to the forum! If you haven't already, please read this topic to learn other useful info for new members. I see you've partially worked out code tags. Please post all code - greater than a line or two - in code tags.

What "issue"? How to detect when the user attempts to divide by zero? What to do about it? It's not clear what you need help with.
Sorry, I guess that was unclear. I mean the program itself is working. As everything else I need it to do.

But. I need to figure out how to get it to display " I'm sorry, you cannot divide by zero " when the user trys to divide by zero.
With what I currently have, eclipse just displays this "
Exception in thread " main " java.lang.ArithmeticException: / by zero
At BasicCalculator.main (BasicCalculator.java:56)
"
Now im just not sure where to go from here.. :/

5. ## Re: Dividing by Zero Issue ( Project - Cant figure out how to do it, or why mine is not working )

how to get it to display " I'm sorry, you cannot divide by zero " when the user trys to divide by zero.
That sounds what could be done with an if statement. Have you tried that?

6. Originally Posted by Norm
That sounds what could be done with an if statement. Have you tried that?
I have an if statement for it. Its just not working.

My current if statement:
```if ( menuNumber == 4) {
System.out.println ("what is the first number?:")
int number1 = input.nextInt ();
System.out.println("what is the second number?:")
int number2 = input.nextInt ();

System.out println("Answer : " + number1 /number2);

If( number2 == 0)
System.out.println(" You can not divide by zero");```

7. ## Re: Dividing by Zero Issue ( Project - Cant figure out how to do it, or why mine is not working )

Perhaps you should check for the illegal division BEFORE it occurs.

8. ## The Following User Says Thank You to GregBrannon For This Useful Post:

Famous112 (September 6th, 2014)

9. Originally Posted by GregBrannon
Perhaps you should check for the illegal division BEFORE it occurs.
It worked!!!
Thank you!!!!