Forum Stats

  • 3,770,456 Users
  • 2,253,116 Discussions
  • 7,875,463 Comments

Discussions

Displaying Text in the Text Area

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Sep 5, 2016 3:37PM in New To Java

Hi,

I am creating a netbeans application. I am trying to handle square root exception. If the argument to sqrt is positive it should display its result in the text area otherwise, it would display a message that square not possible. Its displaying the result. For instance if the argument is positive (i.e. 25) , it displays its result (i.e. 5) in the text area but if the argument is negative it displays "NaN" in the text area instead of "Square root of negative numbers not possible" . Following is my code:

            strVal=tfValue.getText();

            int val=Integer.parseInt(strVal);

            try{

            double res= Math.sqrt(val);

            taResult.setText(" " + res);

            }catch(ArithmeticException e){

                taResult.setText("Square root of negative numbers not possible");

            }

Some body please guide me about this.

Zulfi.

eudriscabrera-JavaNet

Best Answer

  • Unknown
    edited Sep 4, 2016 2:00PM Accepted Answer
    Some body please guide me about this.

    You continue to make it hard for people to 'guide' you when you don't follow their previous advise to first read the documentation or The Java Tutorials to learn how to use Java.

    All of your questions can be EASILY answered just by reading the documentation or by reading the appropriate tutorial and trying the examples there.

    You have to LEARN how to use Java BEFORE you try to write code that uses it.

    This question is a good example.

    I am trying to handle square root exception. 

    What 'exception' are you talking about? There IS NO EXCEPTION. If you read the Java API for the 'sqrt' function you would see that immediately.

    https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#sqrt(double)

    public static double sqrt(double a)

    Returns the correctly rounded positive square root of a double value. Special cases:

    • If the argument is NaN or less than zero, then the result is NaN.
    • If the argument is positive infinity, then the result is positive infinity.
    • If the argument is positive zero or negative zero, then the result is the same as the argument.

    Otherwise, the result is the double value closest to the true mathematical square root of the argument value.

    Parameters:
    a - a value.
    Returns:
    the positive square root of a. If the argument is NaN or less than zero, the result is NaN.

    Do you see any 'exception' shown in the above doc API description?

    For instance if the argument is positive (i.e. 25) , it displays its result (i.e. 5) in the text area but if the argument is negative it displays "NaN" in the text area instead of "Square root of negative numbers not possible" .

    Of course it displays "NaN" - that is EXACTLY what you told it to display:

     double res= Math.sqrt(val); taResult.setText(" " + res);

    That says do the square root and display the result no matter what that result is. If the result is NaN then display it. If the result is NOT NaN then display it.

    So Java did just what you told it.

    You did NOT say 'check the result and if it is NaN display some message instead.

    }catch(ArithmeticException e){ taResult.setText("Square root of negative numbers not possible"); }

    Do you see any 'ArithmeticException' listed in the doc API quote I provided above?

    Do you see where the doc says that the result can be NaN?

    You will continue to have problems with Java, and everything else, until you learn how to use Java.

    The easiest way to do that is to use The Java Tutorials and the Java API docs.

    Let those docs 'guide' you. Then if you have SPECIFIC questions about what those docs say or about one of the trail examples post your question and ask it.

    eudriscabrera-JavaNet

Answers

  • Unknown
    edited Sep 4, 2016 2:00PM Accepted Answer
    Some body please guide me about this.

    You continue to make it hard for people to 'guide' you when you don't follow their previous advise to first read the documentation or The Java Tutorials to learn how to use Java.

    All of your questions can be EASILY answered just by reading the documentation or by reading the appropriate tutorial and trying the examples there.

    You have to LEARN how to use Java BEFORE you try to write code that uses it.

    This question is a good example.

    I am trying to handle square root exception. 

    What 'exception' are you talking about? There IS NO EXCEPTION. If you read the Java API for the 'sqrt' function you would see that immediately.

    https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#sqrt(double)

    public static double sqrt(double a)

    Returns the correctly rounded positive square root of a double value. Special cases:

    • If the argument is NaN or less than zero, then the result is NaN.
    • If the argument is positive infinity, then the result is positive infinity.
    • If the argument is positive zero or negative zero, then the result is the same as the argument.

    Otherwise, the result is the double value closest to the true mathematical square root of the argument value.

    Parameters:
    a - a value.
    Returns:
    the positive square root of a. If the argument is NaN or less than zero, the result is NaN.

    Do you see any 'exception' shown in the above doc API description?

    For instance if the argument is positive (i.e. 25) , it displays its result (i.e. 5) in the text area but if the argument is negative it displays "NaN" in the text area instead of "Square root of negative numbers not possible" .

    Of course it displays "NaN" - that is EXACTLY what you told it to display:

     double res= Math.sqrt(val); taResult.setText(" " + res);

    That says do the square root and display the result no matter what that result is. If the result is NaN then display it. If the result is NOT NaN then display it.

    So Java did just what you told it.

    You did NOT say 'check the result and if it is NaN display some message instead.

    }catch(ArithmeticException e){ taResult.setText("Square root of negative numbers not possible"); }

    Do you see any 'ArithmeticException' listed in the doc API quote I provided above?

    Do you see where the doc says that the result can be NaN?

    You will continue to have problems with Java, and everything else, until you learn how to use Java.

    The easiest way to do that is to use The Java Tutorials and the Java API docs.

    Let those docs 'guide' you. Then if you have SPECIFIC questions about what those docs say or about one of the trail examples post your question and ask it.

    eudriscabrera-JavaNet
  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Sep 5, 2016 1:02PM

    Hi,

    My friend thanks for your explanation regarding NaN. Actually I never believe to get NaN for taking square root of negative value. Because this is mathematics & NaN is not part of mathemetics. God bless you for your time.

    However my learning policy does not depend only on reading documents. I dont hesitate to knock any door for seeking knowledge.

    Zulfi.

  • Unknown
    edited Sep 5, 2016 3:37PM
    Actually I never believe to get NaN for taking square root of negative value.

    The Java API, which I showed you, tells you you can get NaN and that you won't get an exception for your use case.

    The Java API for any class or method is EASILY available in NetBeans just by a right-click and selecting to view the Java Doc.

    And for someone that doesn't know what NaN all you need to do is a simple web search for 'mathematics nan' and the wiki is one of the links

    https://en.wikipedia.org/wiki/NaN

    In computing, NaN, standing for not a number, is a numeric data type value representing an undefined or unrepresentable value, especially in floating-point calculations. Systematic use of NaNs was introduced by the IEEE 754 floating-point standard in 1985, along with the representation of other non-finite quantities like infinities.

    See where NaN has been a standard since 1985?

    However my learning policy does not depend only on reading documents. I dont hesitate to knock any door for seeking knowledge. 

    No one should 'depend only' on reading documents.

    But you should ALWAYS include reading documents as the first steps towards learning something.

    I'm just trying to show you, and others, that the docs usually have the answers to the common questions about the basics.

This discussion has been closed.