I am very (very very) new to java and have what I am sure is a simple
problem that I hope someone can help with. I have two .java files. The first
has
public final class FormatSWN {
public FormatSWN() throws Exception {
etc etc
}
etc etc
}
This file compiles without an errors. I am wanting to call the FormatSWN
class from within another file. The code in that file is
class CallSWN {
public static void main(String[] cName) {
FormatSWN();
}
}
This file returns a compile error saying "Cannot resolve symbol" and has the
^ pointing at the start of FormatSWN();
Both these files are in the same directory so that should not be an issue.
I know this will be really simple, but it has me stumped. If someone has the
answer, that would be great
Steve
FormatSWN is a class, it is the "blueprint" for an object of type
FormatSWN. You have to create an *instance* (object) of the class before
tying to call any of its methods. Like so:
FormatSWN myFormatSWNObject = new FormatSWN();
Now you can call methods using your new object:
myFormatSWNObject.aMethodInFormatSWN();
(The identifier 'myFormatSWNObject' is what you decide to call your new
object. The method 'aMethodInFormatSWN' is a method (you) defined in
class FormatSWN.)
/ulf
I also managed to make it work by
try {
new FormatSWN(new String[]{cName[0]});
} catch etc etc
I have one more question if I may. In the FormatSWN class I have a method
called Speakable, that has to use the Vector command to turn something like
123 into one, two, three.
The method is called by
Vector speakable = Speakable(strinput); //strinput is from the call to
FormatSWN (see original post below)
The Speakable method has
Vector Speakable (String[] strinput) {
int i;
int x = 0;
String strword;
Vector strspeakable = new Vector();
for (i=0; i < strinput.length; i++) {
x = Integer.parseInt(strinput[i])
some switch commands on x & a strspeakable.add go here
}
My problem is that the first time in I would expect strinput.length = 3 and
x to equal 1 (if 123 is passed), then 2, then 3 etc, but instead the
strinput.length =1 and x = 123 ?
Can you tell me what is wrong ?
Thanks and sorry for asking obvious questions !
Steve
"Ulf_N" <u...@dontlikespam.se> wrote in message
news:Mbb1e.133348$dP1.4...@newsc.telia.net...
Your result indicate that strinput only contains one string, in the
first element, and that string is "123". It should contain *three*
elements, holding the strings "1", "2", and "3", respectively. (I think
that you perhaps should read a bit more about how classes, objects,
arrays, etc. works in java. It's boring, I know, but it will save you
lots of time. I promise.) /ulf
On style, I would recommend that you use a List instead of a Vector. A
List is a data type from the Collections framework which provides a
much better overall selection of classes and allows you to choose when
to use thread synchronization.
Another point is that you seem to be trying to do something useful in a
constructor. If you aren't trying to create an object to use later,
you might be better off using a static method. I should point out that
using static methods is often an indication of poor design.