Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes cant find symbol: variable octet1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cant find symbol: variable octet1" Watch "cant find symbol: variable octet1" New topic
Author

cant find symbol: variable octet1

Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
I am having troubles accessing a local variable in the same driven class. Now I realize that you are not able not able to access local variables from outside the method, but just dont see how else I can get the same result.

Here is a sample of my errors:



C:\Documents and Settings\ben\Desktop\park>javac BenHultinProg1.java
.\IpAddress.java:43: cannot find symbol
symbol : variable octet1
location: class IpAddress
this.octet1 = octet1;
^


.\IpAddress.java:61: cannot find symbol
symbol : variable octet1
location: class IpAddress
return octet1;
^




Here is the code I have so far:




and my driver class is:



I appreciate any help in the matter
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Just look at the name and type of your variables. If you don't know what's wrong then read a java tutorial. Good luck

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
well I am not trying to access my octet1Str, that was parsed over to an integer variable named octet1. Which was doing inside the ipAddress () method. My problem is accessing the octet1 int that has been assigned the value I need from inside ipAddress()

I could easily access my octet1 and so forth if I declared them in my heading with the other variables, but then I would only access the default value assigned from there in my setters and getters, correct?

thanks for the help
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I'll say it again: look at the names and types of your variables
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
It's very simple




this is a declaration inside your constructor.

Whatever you declare within a method will vanish after the method has completed.

That means your octet1 to octet4 will just disappear after the constructor has finished.

This is called variable scope. A variable will only last until it reaches it's next pair of curly braces.

That means


so, what you want to do is either make your octet ints an instance variable or have the methods that return the ints do the Integer.parseInt on the octet1Str - octet4Str instance variables.

JDBCSupport - An easy to use, light-weight JDBC framework -
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
I took a look at each and every variable I typed in. I cant see any typos, the types are declared in the proper place to the best of my knowledge.

I have declared the ints octet1 and so forth inside the method correctly. Variables can hold digits inside them as long as they arnt in the front, so I'm good there.

I went over a tutorial on variables, and it didnt mention any code that I could be missing.

Could you please tell me what it is I am missing?

Thanks for the help
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
I just told you.

octet1 to octet4 are method local variables. They disappear right after the constructor finishes.

How can the getter method then find the variable if it disappeared ???
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Thanks for the help I took a look at the code and made some changes you suggested. while I got no more compiler errors, I got this error when I tried to run the class file.





C:\Documents and Settings\ben\Desktop\park>javac BenHultinProg1.java

C:\Documents and Settings\ben\Desktop\park>java BenHultinProg1
216.27.6.136
13
21
Exception in thread "main" java.lang.NumberFormatException: For input string: "6
.136"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at IpAddress.getOctet3(IpAddress.java:75)
at BenHultinProg1.main(BenHultinProg1.java:16)




Exception handling is still a foreign concept. Any suggestions?

Here is my current code:




and my diver class is still the same:



Thanks a lot for the help
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
Sebastian Janisch wrote:I just told you.

octet1 to octet4 are method local variables. They disappear right after the constructor finishes.

How can the getter method then find the variable if it disappeared ???



sorry didnt know you posted that last one, when I was replying earlier.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Take a look at the substring API docs.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Ok,

the reason why it fails now is because, as David said, you use substring incorrectly.

For IP addresses however, you don't want to use substring the way you do.

what if tomorrow you change your IP address to 216.2.634.136 instead of 216.27.6.136 ?? Then your indexing is wrong.

What you want to do is split the string by the dot into an array. The String.split method will help you here. Note that the dot is a special character in regular expressions so you will have to escape it with backslashes.


As for your implementation, you did not understand the concept of instance variables and method local variables.

What you do here is called variable shadowing and is considered evil practice.

Here are your options.

  • Create instance variables that hold the integer value for an octet and use those variables in your getter methods

  • or
  • Do NOT create instance variables and parse the string octet into an integer and return those
  •  
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: cant find symbol: variable octet1