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 17 of 17

Thread: Division by Zero

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Division by Zero

    Hello everybody ,

    I was messing around with java and when I tried to divide a double(decimal) number by 0 ; it was giving me a Infinity response ; does anyone know how to override that infinity response so it can output something like " Why did u just divide by 0 Try Again " . I think it has something to do with the arithmetic expression like so .

    catch(ArithmeticException e)
            {
            System.out.println(beep)
           return " Why did u just divide by 0 Try Again ";
     
                }
    but It is not outputing that message someone has a solution ?

    Also I don't really know it is more of a question or a "what's wrong with my code " so let me know if i am in the wrong section

    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Division by Zero

    Can you post the full text of the error message you get when you divide by 0?

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Division by Zero

    What you will have to do is check if the divisor is zero then throw the exception.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Division by Zero

    Thanks for your response
    I do not get a error message as a response I get "infinity" but I want to be able to write my own response instead of the default one

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Division by Zero

    Did you read my reply?
    Improving the world one idiot at a time!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Division by Zero

    You need to put some more ink in your pen.

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Division by Zero

    Just tried this still not working :
    if (divisor == 0) throw new java.lang.ArithmeticException("Why did u just divide by 0 Try Again ");

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Division by Zero

    You need to put some more ink in your pen.
    Is that a euphemism?
    Last edited by Junky; December 6th, 2011 at 05:23 PM.
    Improving the world one idiot at a time!

  9. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Division by Zero

    Quote Originally Posted by mael331 View Post
    Just tried this still not working :
    That provides us zero information. Without context we have no idea what is wrong. As it is that code should work but without the surrounding code and examples of input we have no idea what the problem is.
    Improving the world one idiot at a time!

  10. #10
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Division by Zero

    What my response to the division or to your post ?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Division by Zero

    The posted code throws the exception for me.

  12. #12
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Division by Zero

    I got it figured out :
    I created a class called MydivisionException; and
    if (divisor == 0) throw new MyDivisionException("Why did u just divide by 0 Try Again ");
    Thanks for your help

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Division by Zero

    Sounds like a wonderful solution.

  14. #14
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Division by Zero

    Quote Originally Posted by mael331 View Post
    if (divisor == 0) throw new MyDivisionException("Why did u just divide by 0 Try Again ");
    The only difference between that and the previous snippet is the use of a different class. If the last snippet didn't work then this should also not work. You must have changed something else in the code which you will not show.
    Improving the world one idiot at a time!

  15. The Following User Says Thank You to Junky For This Useful Post:

    KevinWorkman (December 6th, 2011)

  16. #15
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Division by Zero

    The MyDivisionByZeroException I created
    public class MyDivisionByZeroException  extends Exception {
     
    	    public MyDivisionByZeroException(String strMessage)
    	    {
    	    	super (strMessage);
    	    }
    }

    and this is what i have in my main program

    if (divisor==0) throw new MyDivisionByZeroException(Why did u just divide by 0 Try Again);

  17. #16
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Division by Zero

    Quote Originally Posted by mael331 View Post
    The MyDivisionByZeroException I created
    public class MyDivisionByZeroException  extends Exception {
     
    	    public MyDivisionByZeroException(String strMessage)
    	    {
    	    	super (strMessage);
    	    }
    }

    and this is what i have in my main program

    if (divisor==0) throw new MyDivisionByZeroException(Why did u just divide by 0 Try Again);
    That's not really enough code to judge what's going on. How are you using divisor? Where is that if statement? It would be very helpful if you could provide an SSCCE that demonstrates exactly what's going on.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  18. #17
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Division by Zero

    Once again a single line of code is meaningless. It is the surrounding code and how you use that line which is important.
    Improving the world one idiot at a time!

Similar Threads

  1. Variable Division Issues
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 20th, 2011, 07:57 PM
  2. [SOLVED] Performing Division with Double Variables
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 5th, 2011, 05:36 PM
  3. division problem
    By vgenopoulos in forum Java Theory & Questions
    Replies: 1
    Last Post: July 30th, 2010, 01:51 PM
  4. [SOLVED] Java program to write a code to detect divisibility of any number by 11
    By Java'88 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 11th, 2009, 10:41 PM