Re: Problems with Constructor Methods

From: Hal Rosser (hmrosser_at_bellsouth.net)
Date: 11/27/04


Date: Fri, 26 Nov 2004 22:50:03 -0500


"JS" <dsa.@asdf.com> wrote in message news:co8iu3$noa$1@news.net.uni-c.dk...
> This code works:
>
> import javax.swing.*;
> public class HelperClass
> {
> public void computeSquareRoot()
> { String s = JOptionPane.showInputDialog("Type a number:");
> double d = new Double(s).doubleValue();
> double root = Math.sqrt(d);
> JOptionPane.showMessageDialog(null,
> "The square root of " + d + " is " + root);
> }
> }
>
> class test
> {public static void main(String[] args)
> {HelperClass writer = new HelperClass();
> writer.computeSquareRoot();
>
> }}
>
> But I am trying to make another which calculates the length of two names
and
> show the names and their length in a dialog windows. But I can't get it to
> work:
>
> class NameLength
> { public static void main(String[] args)
> { HelperClass c = new HelperClass();
> c.readNameAndDisplayItsLength();
> c.readNameAndDisplayItsLength();
> JOptionPane.showMessageDialog(null, "Finished!");
> }}
>
>
> class HelperClass
> {
> public void readNameAndDisplayItsLength()
> { String a = JOptionPane.showInputDialog("Type a name:");
> int z = a.length();
> JOptionPane.showMessageDialog(null, a + " Length is " + z);
> }
> }
>
>
> I guess class test and class NameLength has the same function. But why
then
> has NameLength the line JOptionPane.showMessageDialog(null,
> "Finished!");
>
> And why does it have two c.readNameAndDisplayItsLength();
>
> When i write: java NameLength I get the following error:
>
> NoSuchMethodError: HelperClass.readNameAndDisplayItsLength()V
> at NameLength.main(Johannes_Serup_48.java:149)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
> at java.lang.reflect.Method.invoke(Unknown Source)
>

without looking at the whole thing -
it appears you have 2 classes with the same name (HelperClass) - I stopped
at that point

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.799 / Virus Database: 543 - Release Date: 11/19/2004


Relevant Pages

  • Re: break out of continuous for loop in key down event?
    ... public void Run ... private void _Thread ... IMHO, this provides a nice concise way to declare simple logic like setting a boolean, but of course you could declare a named method and use that as the event handler in the traditional way. ...
    (microsoft.public.dotnet.languages.csharp)
  • Inconsistant null parameter handling in Reflection
    ... It seems that the way reflection resolves methods is not quite the same as ... public void Hello{ ... But if I only know the input parameter and the name of the ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to combine this code in on main class?
    ... then you'll need to instantiate the class ... before calling it. ... > public void printEven ...
    (comp.lang.java.help)
  • a little problem whit java code
    ... QUESTION is how can I print out (class Test). ... public Car(String model, String color, double power){ ... public void setModel ... public void addCar(String model, String color, int power){ ...
    (comp.lang.java.help)