Results 1 to 16 of 16

# Thread: need some heLp T.T

- 07-28-2010, 05:36 AM #1Member
- Join Date
- Jul 2010
- Posts
- 16

- Rep Power
- 0

## need some heLp T.T

the build output is

C:\Documents and Settings\STI\My Documents\SqrtException.java:22: sqrt(double) in java.lang.Math cannot be applied to ()

Result = Math.sqrt();

^

1 error

Process completed.

import javax.swing.*;

import java.lang.Math.*;

public class SqrtException

{

public static void main(String[] args)throws ArithmeticException

{

int num;

int Result;

String inputString;

inputString = JOptionPane.showInputDialog(null,

"Enter a number to be square-rooted");

num = Integer.parseInt(inputString);

try

{

Result = Math.sqrt();

}

catch(ArithmeticException exception)

{

if (num<0);

}

JOptionPane.showMessageDialog(null,"cannot use negatives");

System.exit(0);

}

}

helP me pLs...

- 07-28-2010, 05:40 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1

- Rep Power
- 26

What

**sqrt()**is expect to do? You want to find the square root of a number, but which?

- 07-28-2010, 05:44 AM #3Member
- Join Date
- Jul 2010
- Posts
- 16

- Rep Power
- 0

1. Write a Java application that throws and catches an Arithmetic Exception when you attempt to take the square roof of a negative value. Prompt the user for an input value and try the Math.sqrt() method on it. The application either displays the square root or catches the thrown Exception and displays an appropriate message. Name the program as SqrtException.java.

**this is the problem...**

- 07-28-2010, 05:45 AM #4Member
- Join Date
- Jul 2010
- Posts
- 16

- Rep Power
- 0

1. Write a Java application that throws and catches an Arithmetic Exception when you attempt to take the square roof of a negative value. Prompt the user for an input value and try the Math.sqrt() method on it. The application either displays the square root or catches the thrown Exception and displays an appropriate message. Name the program as SqrtException.java.

**this is the problem...**

- 07-28-2010, 05:54 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1

- Rep Power
- 26

Yes, but this is nothing to do with Java.

attempt to take the square roof of a negative value

- 07-28-2010, 05:59 AM #6Member
- Join Date
- Jul 2010
- Posts
- 16

- Rep Power
- 0

no i dont even know it ... T.T

- 07-28-2010, 07:11 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1

- Rep Power
- 26

Ok, you cannot find the square root of a negative number. Only the positive numbers. So check the number before find the square root of it.

- 07-28-2010, 07:44 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1

- Rep Power
- 26

I've tried to re-write your code as much as in a simple way. Go through the following and let me know.

Java Code:import javax.swing.*; import java.lang.Math.*; public class SqrtException { public static void main(String[] args) { double num = -1, Result; String inputString; try { while (num < 0) { inputString = JOptionPane.showInputDialog(null, "Enter a number to be square-rooted"); if (inputString != null) { num = Double.parseDouble(inputString); if (num < 0) { JOptionPane.showMessageDialog(null, "cannot use negatives"); } } else { System.exit(0); } } Result = Math.sqrt(num); JOptionPane.showMessageDialog(null, "Square root is: " + Result); } catch (ArithmeticException ex) { System.exit(0); } } }

- 07-29-2010, 02:50 AM #9Senior Member
- Join Date
- Mar 2009
- Posts
- 552

- Rep Power
- 12

LIEZ! The square root of a negative number is a combination of its absolute value and the hypothetical value

*i*. e.g. the square root of 9 is 3*i*. Now, java doesn't deal with that, and most normal people never have to, but its there.

@OP: The problem with your code is that you're not giving Math.sqrt(double) a number to deal with. I blame whoever wrote the assignment - Math.sqrt() doesn't exist, its either Math.sqrt(double) or just Math.sqrt. Anyway, you need to pass num to the sqrt method as the only parameter. You probably want to change the type of your variables (besides the input) to double, or you'll have to add a cast to int. Also, see what happens if the input is invalid (non-numeric value, window is closed instead of having the accept button clicked (probably says "Ok"), etc.) Then handle that exception/problem in the code. Never trust the user to enter valid data.If the above doesn't make sense to you, ignore it, but remember it - might be useful!

And if you just randomly taught yourself to program, well... you're just like me!

- 07-29-2010, 06:46 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1

- Rep Power
- 26

Of course, complex numbers are there. But better to leave it aside at the moment. Since OP dosen't know that he cannot find the root of a negative number in very basis mathematical principles, I don't think it's a good idea to discuss about that here. So I don't want to mention about that at the moment. :)

- 07-29-2010, 09:38 PM #11Senior Member
- Join Date
- Mar 2009
- Posts
- 552

- Rep Power
- 12

I will admit to being slightly hyperactive while writing my last post. Not sure what I had/did, but it was effective :eek:

If the above doesn't make sense to you, ignore it, but remember it - might be useful!

And if you just randomly taught yourself to program, well... you're just like me!

- 07-30-2010, 05:40 PM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1

- Rep Power
- 26

- 01-06-2011, 05:42 AM #13
**Sir..?..what is the possible output of your re-writed program?....**

- 01-06-2011, 03:08 PM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1

- Rep Power
- 26

- 01-07-2011, 08:42 AM #15
I've try that code sir but the problem is we will not be able to use the while loop or the loop sturcture..its because the problem did not tell us to use the loop structure. And Our Instructor said that we will not be able to use the loop structure because all you is need to take the square root of the negative value.

thanks sir for your answer..

i need the code sir that is not uses the loop..

Can u gave me a code that is not uses a loop?..

based on the problem sir.Last edited by richard; 01-07-2011 at 09:01 AM.

**Java Programming is Challenging**:)

- 01-11-2011, 01:48 PM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1

- Rep Power
- 26

Okay, do you have an idea about what the loop does?

## Bookmarks