• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

new to java, basic error

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post your error message? That would help you help us solve your problem.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But it still does not work...
 
Brian R. Wainwright
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
reply
    Bookmark Topic Watch Topic
  • New Topic