473,395 Members | 1,484 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

get user input

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
1 1304
Nepomuk
3,112 Expert 2GB
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

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

Similar topics

8
by: hokiegal99 | last post by:
I don't understand how to use a loop to keep track of user input. Could someone show me how to do what the program below does with a loop? Thnaks! ---------------------------- #Write a...
3
by: N?ant Humain | last post by:
I have just begun learning Python so that I can write a simple script to make modification of a file used by another Python script easier. This file is basically a list of regular expressions. What...
0
by: Michael | last post by:
Hi All, I am working on my intranet reporting web site, by use of IIS5 + Office2000 +SQL2000. Currently, the first step, I show user an ASP web-interface to collect user input, keyword...
17
by: comp.lang.tcl | last post by:
The TCL command I am using will do a command-line action on a PHP script: set cannotRunPHP I have to do it this way as both the TCL script and the PHP script run as CLI. However, "info.php"...
3
by: dei3cmix | last post by:
Hey, I am having a problem with a program I am working on. Basically, the first part of the program gets input from a file using cin.getline. Then the second part, (still in the same main as the...
2
by: danielboendergaard | last post by:
Hey Im making a homepage in php. I use a html form to put data into mysql and i want to make some buttons which inserts user input values into a textarea. I have used a button like this: <input...
9
by: chuck | last post by:
I need some help with validating user input. I am writing a C computer program for an intro to C course. Here is the situation. I am creating an application that will do currency conversions. ...
2
by: Matt | last post by:
Hi, I'm ridiculously new to Access (about a week!) so please be patient! My database is a record of British Standards. Each has a unique identifier. Some are split into parts. I would like...
2
by: Andy | last post by:
Hi guys, I'm writing a program with a feature of accepting user input as command text and parsing it to correct function calls...example: "5 minutes later"/"5 min later"/"5 minute...
12
by: Tarique | last post by:
I have tried to restrict the no. of columns in a line oriented user input.Can anyone please point out potential flaws in this method? btw.. 1.I have not used dynamic memory allocation because...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.