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
}
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top