Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Add a letter to the end of a variable name

 
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.
 
lowercase baba
Posts: 12856
52
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.
 
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
 
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic