Prob with TextArea in Connection with Sqroot Exception

  • Context: Java 
  • Thread starter Thread starter zak100
  • Start date Start date
  • Tags Tags
    Connection
Click For Summary

Discussion Overview

The discussion revolves around handling exceptions related to the square root function in a NetBeans application. Participants explore how to properly manage cases where the input to the square root function is negative, focusing on the appropriate error handling and messaging in a text area.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes their implementation of square root handling, noting that it displays "NaN" for negative inputs instead of a custom error message.
  • Another participant questions the logic behind the error handling, pointing out that Math.sqrt() does not throw an exception but returns NaN for invalid inputs.
  • A suggestion is made to check the input value before calling Math.sqrt() to avoid the issue with negative numbers.
  • There is a recommendation to change the variable type from int to double to accommodate the requirements of Math.sqrt().

Areas of Agreement / Disagreement

Participants generally agree that the current implementation does not handle negative inputs correctly, and there is a consensus on the need for a different approach. However, there are differing views on how to implement the solution effectively.

Contextual Notes

The discussion highlights the limitations of the current implementation, particularly the misunderstanding of how Math.sqrt() handles negative inputs and the need for proper type handling when passing arguments to the function.

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   Reactions: 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
}
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 3 ·
Replies
3
Views
13K
  • · Replies 80 ·
3
Replies
80
Views
10K
  • · Replies 87 ·
3
Replies
87
Views
9K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K