aspose file tools*
The moose likes JavaFX and the fly likes using variables within a variable 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 » JavaFX
Bookmark "using variables within a variable" Watch "using variables within a variable" New topic
Author

using variables within a variable

Dave Singh
Greenhorn

Joined: Feb 02, 2011
Posts: 5
Hi, just a quick specific question;

Basically, I have a class called Routers in RouterATable.fx. In this file, I have defined routers from RouterB to RouterO, which all store information such as Destination IP, Source IP etc.

I also have a string which stores the names of all the routers:



In another file, called RouterCalculator, I am trying to call information from each router using a while loop in the following way:



This doesnt work because of the Routers[a] bit. If i rewrite that code with RouterATable.RouterB.IP, thats fine, even though Routers[a] = RouterB at the first instance of the while loop, it doesnt work. Any help please ??? Thanks a lot!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Welcome to the Ranch, Dave!

I'm missing the declaration for "a" here. Can you show it?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dave Singh
Greenhorn

Joined: Feb 02, 2011
Posts: 5
Thanks! I'm sure i'll be using these forums a lot over the next couple of months as this software project is actually my dissertation! And maybe I can help some others trying to grasp JavaFX as well lol :-)

the code looks like this:


The compiler error is as follows:

init:
deps-jar:
C:\Documents and Settings\Hav\My Documents\NetBeansProjects\firstDisso\src\firstdisso\RouteCalculater.fx:23: cannot find symbol
symbol : variable IP
location: class String
if (RouterATable.Routers[a].IP == myPacket.DestIP){
C:\Documents and Settings\Hav\My Documents\NetBeansProjects\firstDisso\src\firstdisso\RouteCalculater.fx:25: cannot find symbol
symbol : variable IP
location: class String
println(RouterATable.Routers[a].IP);
2 errors

Thanks again!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

Please UseCodeTags next time. I've added them for you this time (to both posts).

The error is pretty clear, isn't it? Routers contains Strings, and therefore its elements don't have an IP property. You'll need to put Router objects in the array, not Strings.
Dave Singh
Greenhorn

Joined: Feb 02, 2011
Posts: 5
Ahh I think you misunderdstand. I realise Routers[a] is just a string, but its value is (in the first instance of the while loop when a=0) RouterB, which is the name of the array which I'm trying to call IP from. The reason I am doing this is to cycle through each router checking the IP to see if it matches myPacket.destIP. I have arrays from RouterB to RouterO defined in RouterATable. Hope this is clearer now, thanks again for your interest!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

But the routers in Routers don't have an IP. They are just names, as Strings. And therefore you can only treat them as Strings, not as routers. You need to convert the names to IP addresses somehow. If the IP addresses are fixed and known you can create a new class for it, and give it properties for the name and IP.
Dave Singh
Greenhorn

Joined: Feb 02, 2011
Posts: 5
But that would create the same problem. If I had a class called IP and each instance was called IPA IPB IPC IPD IPE etc. etc., how could i create a while loop that would check each array instead of manually writing an if statement for each array?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19672
    
  18

You just replace your Routers array with an array of objects that do have the IP property. Assuming you name this class Router and the IP is determined automatically from the host name, all you need is this:
Now Routers[a] will be an instance of Router and it will have a property called IP, so Routers[a].IP can be called.
Dave Singh
Greenhorn

Joined: Feb 02, 2011
Posts: 5
Ahh i see, that would indeed work. However i've just spoken with my tutor and he has suggested I use a MySQL table to store the routing table for each router. This makes a lot of sense, i should've thought of it in the first place! Also, in using a database, my router simulator will also be more similar to the algorithms used in real life systems.

Thanks again for your help though, much appreciated!
 
Don't get me started about those stupid light bulbs.
 
subject: using variables within a variable