Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Class in another file

1,871 views
Skip to first unread message

S. McKee

unread,
Mar 26, 2005, 5:36:12 AM3/26/05
to
Hi,

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


Ulf_N

unread,
Mar 26, 2005, 5:54:36 AM3/26/05
to
S. McKee skrev:

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

S. McKee

unread,
Mar 26, 2005, 7:05:59 AM3/26/05
to
Thanks !!

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...

Ulf_N

unread,
Mar 26, 2005, 12:28:30 PM3/26/05
to
S. McKee skrev:
<snip>

>
> 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 ?
>

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

Ted Dunning

unread,
Mar 29, 2005, 1:21:05 AM3/29/05
to

You should look at what the difference between the string "123" and the
value returned from "123".split("") is. The first is a string with
three characters and the second is an array that contains four strings,
"", "1", "2", "3". Contemplation on the difference should be
illuminating.

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.

0 new messages