Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Finding a Divide By Zero Error with Doubles

    I am trying to create a calculator and I cannot figure out how to bring up a divide by zero error using doubles.

    else if(temp == '/'){
    String test = Double.toString(var2);
    char one = test.charAt(0);
    if(one == '0'){
    System.out.print("aaa");
    text.setText("Cannot Divide By Zero Err");
    }else
    var2 = var1 / var2;


    The system.out is just a test to see if it goes into the if statement and it does.

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Code:
    if ( Double.parseDouble(divisor.getText()) == 0 ) {
      // divisor is zero !!
    }
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow


  •  

    Posting Permissions

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