File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes new to java, basic error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "new to java, basic error" Watch "new to java, basic error" New topic
Author

new to java, basic error

Eric Java
Greenhorn

Joined: Feb 26, 2004
Posts: 4
Hi, I need some help guys. I am getting an error with this code:
class SquareRoot
{
public static double sqrt(double a)
{
double g, h;

//throws exception if 'a' is negitive
if(a = 0)
{
throw new ArithmeticException("cannot process negitive numbers");
}

while(true)
{
if(Math.abs(g - h) < 1.0e-9)
{
return g;
}
else
{
g = (g + h)/2;
h = a/g;
}
}
}
}
class SquareRootNumbers
{
public static void main(String args[])
{
for(int number = 1; number <= 9; number++)
{
System.out.println(number + " " + SquareRoot.sqrt(number));
}

try
{
System.out.println("-1 " + SquareRoot.sqrt(-1));
}
catch (ArithmeticException ignore)
{
System.out.println(ignore);
}
}
}

Thanks for the help guys!
Srivani Nallan
Greenhorn

Joined: Apr 11, 2003
Posts: 16
Can you post your error message? That would help you help us solve your problem.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Two errors I see immediately: first, the "if (a = 0)" should be "if (a == 0)". Second, inside the while loop, "g" and "h" are used before they've ever been assigned to, and that's illegal in Java; you should initialize them to 0; i.e.,
int g=0, h=0;
There may well be more errors, but there's two to get you started.


[Jess in Action][AskingGoodQuestions]
Brian R. Wainwright
Ranch Hand

Joined: Aug 12, 2003
Posts: 92
this may cause an error as well no?
while(true)
That while loop will keep executing until you get a stack overflow.
--BW
Eric Java
Greenhorn

Joined: Feb 26, 2004
Posts: 4
Um thanks guys. I set g and h equal to zero and added the extra equal sign. Now, it compiles, but there is this "NoclassDef" error!? Do you know what I need to do? thanks for all the help.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Brian R. Wainwright:
this may cause an error as well no?

Well, no. There's a return in the middle of the loop; when the appropriate condition is met, the loop terminates. while (true) loops (or the for-loop variant "for (; " are quite common, actually.
Also note that iterating in a loop doesn't create stack frames, so even a loop like

doesn't cause a stack overflow; it just wastes a lot of CPU time.
Eric Java
Greenhorn

Joined: Feb 26, 2004
Posts: 4
But it still does not work...
Brian R. Wainwright
Ranch Hand

Joined: Aug 12, 2003
Posts: 92
Ah.... right. I missed the return
As to the error is the SquareRoot class in the same package or being imported by the SquareRootNumbers class?
Eric Java
Greenhorn

Joined: Feb 26, 2004
Posts: 4
Well I am not sure I understand... I want to pull the sqrt stuff and basicly use the input in the main class? I am not very good at this. help please!
Brian R. Wainwright
Ranch Hand

Joined: Aug 12, 2003
Posts: 92
Hi Eric,
Essentially what I think is happening is that the SquareRootNumbers class is trying to use a class that it doesn't know exists, namely the SquareRoot class. You can educate a class as to the where-abouts of other classes through "import" statements. Import statements must appear at the top of a class file immediately after any package declaration (if one exists). By default, all java classes import all the classes in the java.lang package. Here's an example:

How you import classes is usually determined by your organization (whether to import all classes in a package by using the * or whether to specify the specific classes you need. Personally I prefer to be as specific as possible sinces knowing what specific classes are in use, helps to understand the code.) The important thing to note is that any classes you are attempting to import, must be in the CLASSPATH of the JVM that is using them. This is typically your system CLASSPATH, although it can be specified on the command line through the -classpath option of the java executable

Likewise, the java compiler (javac) looks to a classpath to find class files when compiling. Sometime you may want to specify the -sourcepath option of javac to tell the compiler where is should look for source files

This isn't usually necessary however, as the compiler looks for source files the same place class files are searched for - namely the classpath.
So in your case I would do the following.
Add SquareRoot.class and SquareRootNumbers.class to your classpath.
In your SquareRootNumbers.java file add the following line at the top of the file:

Luck.
BW
[ February 27, 2004: Message edited by: Brian R. Wainwright ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Can you please post the exact error message word for word? As you can see, everyone is trying to guess how to fix the problem without know what the problem actually is. If you give us the error message, it will greatly minimize the guessing game, and we will be able to help you more effectively.
(Hey, guys, the OP is still struggling with a compiler/environment error. I don't think we need to investigate the run-time/logic errors in the code until we fix the most pressing problem. --but that's just my two cents.)
[ February 27, 2004: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Eric Java,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: new to java, basic error