2 simple questions

I have an applet, a main class and an abstract class (Type) with 6 subclasses (Types 1-6).

(I'm aware it's said applets shouldn't really be used by beginners but I have sufficient tutorials for what I'm setting up.)

I'm having trouble with parsing the input strings of the applet's textfields into integers because when the applet is run, the textfields are required to be blank. How can I fix this? I read that you can add try catch exceptions but I'm not sure how in this context.

Here's a snippet:

Label costLbl = new Label("Input cost: ");

TextField costTxt = new TextField(10);

int cost = Integer.parseInt(costTxt.getText());

2nd question:

I know that public variables are accessible to every class in the program, I just don't know how to get them to/reference them in the different classes so I can use them for calculation and 'send' them back with final answers. For example how can I use the input variables from my applet within my abstract class for calculation?

[1041 byte] By [Yuriy_Ivanova] at [2007-11-26 14:27:45]
# 1

In response to the first question:

the overall layout of a try and catch routine would be like this:

try {

thecommandwhichcangenerateerrors;

}

catch (Exception e) {

System.out.println("There was an error with the command!");

}

Concerning the second question:

I don't think it's possible to use variables from child classes (which are not present in the abstract parent class) in your parent class.

Not sure about that though, so anyone confirming this would be nice..

Tribioa at 2007-7-8 2:21:23 > top of Java-index,Java Essentials,New To Java...
# 2

I'm getting an 'illegal start of type' error.

I declared the routine straight after this:

class costCalculation extends Applet implements ActionListener {

//declared here

And my next label says 'identifier expected'.

Yuriy_Ivanova at 2007-7-8 2:21:23 > top of Java-index,Java Essentials,New To Java...
# 3

It shouldn't be put there, since that's the definition of your class, and all the methods you use in the class..

The try catch should be put in the main proportion of the class (where you type the main code of the program).

For instance:

class costCalculation extends Applet implements ActionListener {

// declaration of variables and methods

public static void main(String[] args) {

// TODO code comes here

try {

....

}

catch (Exception e) {

....

}

}

}

Tribioa at 2007-7-8 2:21:23 > top of Java-index,Java Essentials,New To Java...
# 4

I didn't think applets could have a main method, (unless I misunderstood) so I tried putting in the init() method I have where the components are added.

It fixed the try catch errors but now it's causing problems with:

public void actionPerformed(ActionEvent e) {

Since I put it in another method, the above method can't recognise it any longer in order to set the textboxs' text to "" when my cancel button is pressed.

Yuriy_Ivanova at 2007-7-8 2:21:23 > top of Java-index,Java Essentials,New To Java...
# 5

Hmmz, sorry for the mistake then, my eyes seem to ignore the string applets :)

The problem you have now seems like a scope problem.

You should create a reference to textbox in the class part, not in a method of the class. (You can initialize the textbox value everywhere then, since every method of the class will know what your textbox variable is).

For instance:

public class aClass {

Label costLbl = new Label("Input cost: ");

TextField costTxt = new TextField(10);

public void actionPerformed(ActionEvent e) {

// here you can work with the costTxt variable still, since it's globally

// (don't know if that word is correct to be used here) declared, and

// not in the method.

}

public void anotherActionblabla(ActionEvent e) {

// also here you still can work with the costTxt variable..

}

}

Sorry I can't be of more help to you, I'm learning as we speak..

Hope to be of assistance.

Tribioa at 2007-7-8 2:21:23 > top of Java-index,Java Essentials,New To Java...
# 6
Thanks very much, that seemed to do the trick.
Yuriy_Ivanova at 2007-7-8 2:21:23 > top of Java-index,Java Essentials,New To Java...
# 7
Hehe, you're very welcome.. I'm glad to see that we two noobs managed to work it out.. :)Java ownz!Take care..
Tribioa at 2007-7-8 2:21:23 > top of Java-index,Java Essentials,New To Java...
# 8
> I didn't think applets could have a main method...They can; it's just that there's not much reason to do so.~
yawmarka at 2007-7-8 2:21:23 > top of Java-index,Java Essentials,New To Java...
# 9

>Hehe, you're very welcome.. I'm glad to see that we two noobs managed to work it out.. :)

>Java ownz!

>Take care..

Thank you. Good luck with future programming.

> They can; it's just that there's not much reason to do so.

Ah, I see, I wasn't aware of that.

So does anyone know the answer to my second question?

Here is a link to an image of the classes, it shows my hierarchy more clearly:

http://img388.imageshack.us/img388/466/classdfh6.jpg

Can my subclasses use the values of the variables collected in the applet in order to calculate costs in the implemented methods from the abstract class? (if that made sense) If so, how is this possible?

If not, I really need to re-design my hierarchy.

Yuriy_Ivanova at 2007-7-8 2:21:23 > top of Java-index,Java Essentials,New To Java...
# 10

> Can my subclasses use the values of the variables

> collected in the applet in order to calculate costs

> in the implemented methods from the abstract class?

> (if that made sense) If so, how is this possible?

If I understand the question correctly, then yes, you can..

You need to make sure that the cross-variables (if I can call 'em that) are declared somewhere where both your applet as your class1 till class6 can access them, and that would be in the abstract class.

(so no need to rewrite your hierarchy, just make sure that the textbox and all stuff is declared in the abstract class)

Sorry if I misunderstood the question..

Tribioa at 2007-7-8 2:21:23 > top of Java-index,Java Essentials,New To Java...
# 11

Hmm, I had to add those variables within the init() method so that the try catch statements would work.

Maybe the term we're looking for here it similar to the concept of global variables since they need to jump between classes.

I'm just not sure of the code to define those variables in other classes so they are recognised.

It wouldn't be such a problem if my interface applet could merge with my abstract class.

Yuriy_Ivanova at 2007-7-8 2:21:23 > top of Java-index,Java Essentials,New To Java...