Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Add a letter to the end of a variable name

 
Tim Porritt
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to add a letter from an array to the end of KeyEvent.VK_

My code so far is as follows:



There's probably a better way to do this, but i don't know how to do it.
I'll add the rest of the alphabet later.

Thank you in advance for any help.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are trying to dynamically create the name of a variable inside your running program, you can't. variable names are used by programmers, not the running code.

Whatever you are REALLY trying to do...there is almost for sure a better way.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as keycode() returns an int, perhaps all you need to do is something like this

and to check
System.out.println(keycode("C"));
System.out.println(KeyEvent.VK_C);
 
Tim Porritt
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What i'm trying to do is get the keycode for a string.
e.g If the string says "a", i want to return the integer for that, which is KeyEvent.VK_A, or 65.
I already found a way to do this, but it's really long and i have to write out every letter of the alphabet 3 times.

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can simplify it a bit
if(i.equals("A") || i.equals("a"))
can be done
if(i.toUpperCase().equals("A"))

or, in my earlier post
return (int)i.charAt(0);
becomes
return (int)i.toUpperCase().charAt(0);

and the test:
System.out.println(keycode("c"));
System.out.println(keycode("C"));
System.out.println(KeyEvent.VK_C);

all should print 67
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic