Prob with TextArea in Connection with Sqroot Exception

  • Java
  • Thread starter zak100
  • Start date
  • Tags
    Connection
In summary, the code above checks the input value to make sure that it is not negative, and then takes the square root of that value.
  • #1
zak100
462
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
  • #2
What would Math.sqrt() have to do in order for your error message to get printed?
 
  • #3
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
  • #4
Okay. Thanks. Thats why its not coming in the catch block.
I must use a different technique.
Zulfi.
 
  • #5
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
}
 

Related to Prob with TextArea in Connection with Sqroot Exception

1. What is a TextArea and how is it used in connection with a Sqroot Exception?

A TextArea is a graphical user interface element that allows users to input and display text. In connection with a Sqroot Exception, a TextArea is commonly used to display error messages or stack traces when an error occurs during the calculation of a square root.

2. What is a Sqroot Exception and why does it occur?

A Sqroot Exception is an error that occurs when trying to calculate the square root of a negative number. This is because the square root function is not defined for negative numbers in mathematics. As a result, the computer cannot perform the calculation and throws an error.

3. How can I handle a Sqroot Exception when using a TextArea?

One way to handle a Sqroot Exception when using a TextArea is to use a try-catch block in your code. This will allow you to catch the Sqroot Exception and display a custom error message in the TextArea, instead of the default error message.

4. Can a Sqroot Exception be prevented?

Yes, a Sqroot Exception can be prevented by checking for negative numbers before performing the square root calculation. You can use an if statement to check if the input value is negative, and if so, display an error message in the TextArea instead of attempting the calculation.

5. Are there any other ways to handle a Sqroot Exception besides using a TextArea?

Yes, there are other ways to handle a Sqroot Exception besides using a TextArea. For example, you can use a label to display the error message, or you can use a pop-up dialog box. Ultimately, the method of handling the exception will depend on the specific needs and design of your program.

Similar threads

  • Beyond the Standard Models
Replies
3
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Math Proof Training and Practice
3
Replies
80
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
12K
  • Quantum Physics
3
Replies
87
Views
5K
  • Special and General Relativity
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • STEM Academic Advising
Replies
13
Views
2K
Replies
2
Views
2K
Back
Top