# Using an existing char variables stored value to call a method on a vector?

• 09-09-2011, 02:40 AM
itslikeanegg
Using an existing char variables stored value to call a method on a vector?
Hey guys, here's the problem I have. I'm writing a Towers of Hanoi problem for a course and I have the recursive solution already complete. What I'm trying to do is visually show the user which number disk is on which poll for every loop.

A.........B.........C
123
A.........B.........C
23.......1
...and so on until....
A.........B.........C
......................123

The way I'm doing it is I have three vectors named A,B,C and A is initialized with the 1,2,3 disks. For every pass of the recursive solution I need to do a .remove(disknum) and a .add(disknum) on the correct vector but of course every pass moves a different disk from a different poll. Now the recursive solution is actually fed the fromPeg and toPeg for every pass and I need something that will work like this:

fromPeg.remove(diskNum); toPeg.add(diskNum) but to actually operate on one of my A, B, C vectors representing those polls (as if I was calling A.remove(diskNum); B.add(diskNum)). The value stored by char toPeg and char fromPeg would be either A, B, or C.

How would I go about doing this?

Right now the output is:
Disk 1 from A to C
Disk 2 from A to B
Disk 1 from C to B
Disk 3 from A to C
Disk 1 from B to A
Disk 2 from B to C
Disk 1 from A to C
• 09-09-2011, 03:12 AM
Junky
Re: Using an existing char variables stored value to call a method on a vector?
You posted a lot of details (in comparison to most who provide zero details) but I'm not sure what your problem is.
• 09-09-2011, 03:29 AM
itslikeanegg
Re: Using an existing char variables stored value to call a method on a vector?
Basically I need to use the value of a char to perform an operation on a Vector which has the same name as the char's value.

So instead of calling a vector, A.add(x); I need something equivalent to using the value inside of a char, toPeg.charValue.add(x); in effect using the chars value to call the vector. I need this because the value inside the char is changing with every loop and it dictates which vector needs to be operated on (although the char variables name is static)
• 09-09-2011, 03:35 AM
Junky
Re: Using an existing char variables stored value to call a method on a vector?
It still does not make sense to me and I get the feeling you are going about this the wrong way but why not use an if statement?
Code:

```if (char == 'a' ) {     do this } else if (char == 'b') {     do that }```
• 09-09-2011, 03:44 AM
Norm
Re: Using an existing char variables stored value to call a method on a vector?
Quote:

use the value of a char to perform an operation on a Vector which has the same name as the char's value.
Variable names are assigned when you type in the code. No way to generate one when executing.
To use the value of a variable to find an object, you can use if/else as Junky suggested or use a Map that uses the char's value as the key to get the Vector object.
Or if the char values are contiguous say a-z only then use a 26 element array of Vectors and use the chars value -'a' to index the array. Note you can do arithmetic with chars: 'b' - 'a' = 1