Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to assign a String as a char varable name  RSS feed

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a number of char varable names i.e.

char pos1;
char pos2;
char pos3;
char pos4;
char pos5;
char pos6;
char pos7;

I thought the above was a little long winded so I decided to use a for loop and assign the varable names to char i.e.




Where I am having difficulty is working out how to assign the variableName which is a String to char i.e. char variableName. It is saying variableName is already defined; Of which I want it to be! But how do I say it is a String which is the name of a char variable?
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Maureen,

If I'm not mistaken (correct me if I'm wrong), you cannot have dynamic variable names in Java. But you can do that in JavaScript though.

HTH.
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chengwei Lee,

Many thanks for your quick response.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may be too obvious, but the alternative to having char variables named "pos1", "pos2", "pos3" is to have an array of char named pos, and then work with pos[1], pos[2], and pos[3]. Seems like you're going to great lengths to avoid this simple solution -- how come?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!