Java Prob with TextArea in Connection with Sqroot Exception

  • Thread starter Thread starter zak100
  • Start date Start date
  • Tags Tags
    Connection
AI Thread Summary
The discussion revolves around handling exceptions in a NetBeans application when calculating the square root of a number. The user, Zulfi, is attempting to display the result of the square root operation in a text area, but encounters an issue where negative inputs return "NaN" instead of a custom error message. It is clarified that the Math.sqrt() method does not throw an exception for negative values; instead, it returns NaN. The recommended solution is to check the input value before calling Math.sqrt(). The input should be parsed as a double, and if the value is negative, an appropriate message should be displayed. This approach avoids the need for a try-catch block and ensures correct handling of both valid and invalid inputs.
zak100
Messages
462
Reaction score
11
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.
 
Technology news on Phys.org
What would Math.sqrt() have to do in order for your error message to get printed?
 
If you look at the Math.sqrt() javadoc, you will see that it takes a double. You are passing an int. Also, the method does not throw an exception. If the result is invalid, it returns the double value NaN. You will have to look for that value rather than trying to catch an error.
 
  • Like
Likes jedishrfu
Okay. Thanks. Thats why its not coming in the catch block.
I must use a different technique.
Zulfi.
 
zak100 said:
If the argument to sqrt is positive it should display its result in the text area
That's not quite right. The argument to sqrt() can be zero, in which case the returned value is also zero.

Instead of a try... catch block, have your code test the input value first before it takes the square root.

I would do something like this. Note my change for the type of val from integer to double.
Java:
strVal=tfValue.getText();

double val = Double.parseDouble(strVal);
if (val < 0)
// Display appropriate message for negative input value
else
{
   double res= Math.sqrt(val);
   // Display appropriate value for valid input value
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top