aspose file tools*
The moose likes Beginning Java and the fly likes Problem with passing variables between methods and classes (I think) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with passing variables between methods and classes (I think)" Watch "Problem with passing variables between methods and classes (I think)" New topic
Author

Problem with passing variables between methods and classes (I think)

Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Hi. I keep getting hit with all these "cannot find symbol" issues: Basically I know where the variable "name" is; it's defined in the StringStatement and IntegerStatement classes (in the Directive interface). I'm trying to pass the "name" variable, which appears in different classes, to this method in the class TPLVariables which is supposed to populate the HashMap variables:




However if I try to use that method to print the value of the variable inside any of the *Statement classes e.g. StringStatement, I get
"error: cannot find symbol: System.out.println(TPLVariable.getVariable(name))"... Is this a scope issue? Also I'm not sure why but the program seems to think that variablesHash is a variable when it's a HashMap-- and it can't find that either. I'm getting 18 errors on compile (down from a whopping 28, huzzah!) and it's all from these two things. What's wrong and how do I get it fixed?







"How do you define fool?"
"I don't attempt it. I wait for demonstrations. They inevitably surpass my imagination."
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Noam Ingalls wrote:
However if I try to use that method to print the value of the variable inside any of the *Statement classes e.g. StringStatement, I get
"error: cannot find symbol: System.out.println(TPLVariable.getVariable(name))"... Is this a scope issue?


Yes. The "name" variable is a local variable of the addVariable method, so it exists only during the body of that method. What do you expect name to mean where you're trying to use it inside execute()? Where do you think it should come from?

Also I'm not sure why but the program seems to think that variablesHash is a variable when it's a HashMap


No, it's a variable. A variable whose value is, I assume from your above comment, a reference to a HashMap.

-- and it can't find that either.


Then again you have a scoping problem. You're trying to use it in a scope where it doesn't exist. (Or perhaps you're just misspelling it.)

I'd suggest you study up on member variables, local variables, and scope.

Perhaps here:
http://docs.oracle.com/javase/tutorial/java/

Or in whatever book, website, or google search you prefer. It's fundamental stuff, and is explained more thoroughly elsewhere than somebody could get into here. And I would further suggest that you set this program aside for a bit and just write a brand new, small program just to play around with variables and their scopes and get yourself familiar with those concepts without other stuff getting in the way.

Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Huh. The thing is, the information that the TPLVariable class and its subclasses needs to work actually comes from the *Statement classes, which is why I was trying to get that particular value passed into the TPLVariable class. Not possible?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Noam Ingalls wrote:Huh. The thing is, the information that the TPLVariable class and its subclasses needs to work actually comes from the *Statement classes, which is why I was trying to get that particular value passed into the TPLVariable class. Not possible?


Well, yes, it's definitely possible to get information from one class into another. I'm not paying enough attention to your overall design to properly suggest whether you should pass it as a a method parameter, make it available via getters, redesign your classes so the right information is in the right place in the first place, or something else, but it's definitely possible.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Noam, I know you're not going to want to hear this, but I think the project you've taken on is too big relative to your current understanding of Java. There's a lot of very basic stuff that you're still learning, and that stuff is best learned in an isolated context in a small program.

I strongly, strongly suggest that each time you get to a language or API concept that's giving you trouble, that you set aside this project and write a few tiny programs just for the purpose of studying that concept. Just use dummy objects like Animals or Fruits or whatever simple thing strikes your fancy. Hardcode stuff. Use simple System.out.println() calls. Clear away everything that might be distracting you from learning that new concept. Then, when you've got a solid grasp, go back and figure out how that fits into your main project.

I know it may seem like you'll be wasting time writing these other little programs, but I promise you, you'll learn faster and with less headaches, and in the end, you'll spend less time and have a better final result. Additionally, it will be easier for folks here to help you if you can present tiny SSCCEs that focus only on the concept you're struggling with and have no distracting fluff.

Good luck!
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Well, I already knew it was too big and I can now say I honestly detest this project; but it's 60% of my coursework grade for this module, which module I can't fail if I intend to progress in my degree, I'm a whole WEEK overdue and the marks on this thing have just about been deducted to zero, I guess. My final paper for this same module (programming) is tomorrow and I'm trying to finish this thing while cracking my head on the books. Right now I just want to get this program over and done with... At this point I don't know whether to throw in the towel and send in this already horrendously late submission with only partial functionality, or try and fix the problem...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Noam Ingalls wrote:At this point I don't know whether to throw in the towel and send in this already horrendously late submission with only partial functionality, or try and fix the problem...


I can't give you any advice there, but I feel your pain. We've all been there at one point--deadline fast approaching (or past) and a mess in front of us with no time to start over and little hope of successfully plowing forward with what we've got. I actually still have nightmares about it, and when I wake up, I'm always so glad I'm not in school any more, and have no plans of going back.

Good luck with it, and if you have more Java questions, continue to post them, of course, and folks here will do what we can to help with that aspect of it.
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

All right. Well my dad raised no quitters. In for a penny in for a pound and all that-- I doubt my lecturer would be amused at a submission with partial functionality waaay over the deadline. My major issue now is with the parser classes-- StringStatement, IntegerStatement and LetStatement and CalculateStatement. I can handle Strings that have been declared in double quotes no problem, but it's handling variables that's the trouble. Now the Statement classes are supposed to handle all the data parsing. The data such as variable names and variable values go into a fnction called addVariable() in a separate HashMap called variableHash for easy manipulation. Now if I was going to need to create a new String or Integer variable, would it make more sense to you have all the parsing and instantiating in one place, and then in the *Statement classes, just have the one line to call the function addVariable() in the class?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Noam Ingalls wrote:I can handle Strings that have been declared in double quotes no problem, but it's handling variables that's the trouble.


There's no difference. A String is a String, regardless of where it comes from.

http://www.youtube.com/watch?v=Ijae2WHdc9I


Now the Statement classes are supposed to handle all the data parsing. The data such as variable names and variable values go into a fnction called addVariable() in a separate HashMap called variableHash for easy manipulation. Now if I was going to need to create a new String or Integer variable, would it make more sense to you have all the parsing and instantiating in one place, and then in the *Statement classes, just have the one line to call the function addVariable() in the class?


Sorry, I'm having a hard time picturing what you're talking about.
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Ayup, Strings are Strings. I'll see if I can explain the program structure better: First off the data to be parsed is read into an ArrayList of Strings; next the Strings are parsed as keywords+(variables+ values). So the first parsing takes the keyword in the String, and directs it to the correct class e.g. StringStatement class via a HashMap. Now inside the StringStatement class I need to instantiate a new String from the information left from the first pass: that would be the variable (plus value). Now to aid in manipulating the data a second HashMap, variablesHash, is used to store the variable-value pairs. Variables have to be declared first before values are assigned; you can't assign a vlue to a variable on the same line directly. So inside the TPLVariables class is a second HashMap <String, TPLVariable>. This class also contains the abstract class TPLVariable, with two subclasses TPLInteger and TPLString, a getVariable() and an addVariable() function with the parameters (String name, value). My current question now is-- does it make more sense to instantiate every variable inside the TPLVariables class, and just have the *Statement classes provide a call to the addVariable function as necessary? I'd need to get the values for the String name and String value into the addVariable function somehow though. Thoughts?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
I think your problem is that you are parsing your declaration statement to extract the name and value of the variable and then passing these to the addVariable method. It would be better to pass the whole statement to the addvariable method and let it use the statement to decide what type the variable is (this assumes the declaration statement includes a type for the variable)
Something like


Once you've parsed all your declaration statements you can then pass a reference to the TPLVariables class to the execute method (I think that's what you called it isn't it ?) of all your Statement classes for them to use.
You will need to add an appropriate get method to the TPLvariables class. Something like

You will need to decide whether to validate the variable name in this method or let the calling code handle a null return if the variable doesn't exist.


Joanne
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

Thanks Joanne! That makes a whole LOT more sense than the mess I'm staring at in front of me right now. >.< Speaking of declaration statements it'd have to be the entire String e.g. STRING myString, right? In which case only one array would be needed. But again the first parsing splits the String into parts[0] and parts[1]-- maybe if later on in the classes I'd have to split the substring in parts[1] further by whitespace?
 
 
subject: Problem with passing variables between methods and classes (I think)