• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

using variables within a variable

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 22809
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Dave!

I'm missing the declaration for "a" here. Can you show it?
 
Dave Singh
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22809
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22809
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22809
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic