By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,805 Members | 1,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,805 IT Pros & Developers. It's quick & easy.

get user input

P: 59
i am having two problems. 1st is i want to print 3 char from string. i dont want to print ontop of each other so i am doing 600*i. but this not really working.
Expand|Select|Wrap|Line Numbers
  1. g.drawString(username[i], 600*i, 300);
2nd question is that if user hit 3 keys at same time than in string it will add only one and string will be full. bc user hit 3 times at same time.


full code
Expand|Select|Wrap|Line Numbers
  1. //paint method
  2. for(int i = 0; i < username.length; i++){
  3.     g.drawString(username[i], 600*i, 300);
  4.     System.out.print(username[i]);
  5. }                
  6.  

Expand|Select|Wrap|Line Numbers
  1. int z = 0;
  2. String username[] = new String[3];
  3.  
  4. public void keyPressed(KeyEvent e)
  5.     {
  6.         int keys = e.getKeyCode();
  7.  
  8.         username[z] = KeyEvent.getKeyText(keys);
  9.         e.consume();                  
  10.     }
  11.  
  12. public void keyReleased(KeyEvent e)
  13.     int keys = e.getKeyCode();
  14.  
  15.     z++;
  16.     e.consume(); 
  17. }
May 2 '13 #1
Share this Question
Share on Google+
1 Reply


Nepomuk
Expert 2.5K+
P: 3,112
OK, I'm a little confused...
  1. You say you want to print characters from Strings, but it looks like you're printing whole Strings. From what you say I'd imagine something like:
    Expand|Select|Wrap|Line Numbers
    1. char[] username = {' ',' ',' '};
    2. public void paint(...) {
    3.   //...
    4.   for(int i=0; i<username.length(); i++) {
    5.     g.drawString("" + username[i], 600*i, 300);
    6.     System.out.print(username[i]);
    7.   }
    8.   //...
    9. }
    10.  
    or maybe
    Expand|Select|Wrap|Line Numbers
    1. String username = "";
    2. public void paint(...) {
    3.   //...
    4.   for(int i=0; i<username.size(); i++) {
    5.     g.drawString("" + username.get(i), 600*i, 300);
    6.     System.out.print(username.get(i));
    7.   }
    8.   //...
    9. }
    10.  
    or even
    Expand|Select|Wrap|Line Numbers
    1. String username = "";
    2. public void paint(...) {
    3.   //...
    4.   g.drawString(username.substring(0,3), 600, 300);
    5.   System.out.print(username.substring(0,3));
    6.   //...
    7. }
    8.  
  2. What you could try is synchronizing the keyPressed function; I don't know whether this will work, but it's something you could try. This also means, that an array is no longer an option unless you synchronize that manually; switching to a synchronized collection, e.g. a synchronized ArrayList, is probably the best solution. This is what that would look like:
    Expand|Select|Wrap|Line Numbers
    1. List username = Collections.synchronizedList(new ArrayList());
    2.  
    3. public synchronized void keyPressed(KeyEvent e) {
    4.   int keys = e.getKeyCode();
    5.   username.add(KeyEvent.getKeyText(keys));
    6.   e.consume();
    7. }
    8.  
    If that doesn't work you may want to have a look at Key Bindings.
Does that help?
May 2 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.