aspose file tools*
The moose likes Game Development and the fly likes Add a letter to the end of a variable name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Add a letter to the end of a variable name" Watch "Add a letter to the end of a variable name" New topic
Author

Add a letter to the end of a variable name

Tim Porritt
Greenhorn

Joined: Jul 22, 2012
Posts: 4
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

Joined: Oct 02, 2003
Posts: 11420
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Jul 22, 2012
Posts: 4
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

Joined: Jun 09, 2003
Posts: 4632
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
 
jQuery in Action, 2nd edition
 
subject: Add a letter to the end of a variable name