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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

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

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    17
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question 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 {
    	// Main menu
    	public static void main(String[] args) {
    	//Create Scanner
    				Scanner input = new Scanner(System.in);	
     
     
    	System.out.println("Menu: ");
    	System.out.println("1. Add ");
    	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?");
    	int menuNumber = input.nextInt();
    	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.");
    	}
    	if (menuNumber == 1) {
    		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.print("Answer : "+answer  );
    	}
    	if (menuNumber == 2) {
    		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.print("Answer : " +answer  );
    	}
    	if (menuNumber == 3){
    		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.print("Answer : " + answer  );
    	}
    	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.print("Answer : " + number1 / number2);
     
    	if  (number2 <= 0) {
    			System.out.println("You can Not divide by zero.");
    	} 
    	}
    	if (menuNumber == 5){
    		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);
     
     
    	System.out.println("Answer: " + answer);
    	}
    	}
    	}
    "
    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.
    Last edited by Famous112; September 7th, 2014 at 02:18 PM. Reason: Solved!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    17
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by GregBrannon View Post
    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. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    17
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Norm View Post
    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. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default 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. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    17
    My Mood
    Stressed
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

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

Similar Threads

  1. Issue with calendar project
    By phlegm in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 24th, 2013, 05:16 PM
  2. Dividing inside a for loop. Lolwtf?
    By samy109 in forum Loops & Control Statements
    Replies: 6
    Last Post: July 23rd, 2013, 09:04 PM
  3. [SOLVED] I can't figure out why this isn't working!
    By samjoyboy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2012, 04:04 PM
  4. Calculator dividing by 0 problem.
    By BITmixit in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 18th, 2012, 09:06 PM
  5. my code isn't working an i can't figure out why
    By jack13580 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 18th, 2011, 12:21 PM