Problems with Constructor Methods
From: JS (dsa._at_asdf.com)
Date: 11/27/04
- Next message: Chris Smith: "Re: Problems with Constructor Methods"
- Previous message: Victor Lukoshkin: "Re: ????? instead of cyrilic letters"
- Next in thread: Chris Smith: "Re: Problems with Constructor Methods"
- Reply: Chris Smith: "Re: Problems with Constructor Methods"
- Reply: Hal Rosser: "Re: Problems with Constructor Methods"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 27 Nov 2004 01:50:49 +0100
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)
JS
- Next message: Chris Smith: "Re: Problems with Constructor Methods"
- Previous message: Victor Lukoshkin: "Re: ????? instead of cyrilic letters"
- Next in thread: Chris Smith: "Re: Problems with Constructor Methods"
- Reply: Chris Smith: "Re: Problems with Constructor Methods"
- Reply: Hal Rosser: "Re: Problems with Constructor Methods"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]