• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

How to assign a String as a char varable name

 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chengwei Lee,

Many thanks for your quick response.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic