Discussions
Categories
- 196.8K All Categories
- 2.2K Data
- 238 Big Data Appliance
- 1.9K Data Science
- 450.2K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 544 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.8K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.5K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 154 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 17 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 436 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
Displaying Text in the Text Area

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.
Best 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.
Answers
-
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.
-
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.
-
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.