you mean like getting key presses? try this:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Keys extends Applet implements KeyListener {
int i;
int key;
public void init() {
addKeyListener(this);
}
public void keyPressed(KeyEvent evt) {
key = evt.getKeyCode();
repaint();
}
public void keyReleased(KeyEvent evt) { ; }
public void keyTyped(KeyEvent evt) { ; }
public void paint(Graphics g) {
g.drawString("you pressed "+key, 10, 10);
}
}
this small applet will paint the virtual keycode of the key you pressed. usually the folowing code is used later to do an action when a certan key is pressed: (If you have alot of keys you can do a switch...case statment.)
if(key == VK_ENTER) {
}
the VK_ENTER is a keycode for enter. You can replace it with just about anythying (VK_UP, VK_DOWN, VK_THREE, VK_H, ect)
No not like that. Without applets Like this:
public class something{
public static void main(String[] args){
int i;
System.out.println("How old are you");
//Now the user enters the age
int i = something here which reads the keyboard;
System.out.println("Okey! So you are " + i + " years old");
}
}
Thanx :)
import java.io.*;
public class Test{
public static void main(String[] args)throws Exception{
String line = "";
InputStreamReader isr = new InputStreamReader( System.in );
BufferedReader br = new BufferedReader( isr );
System.out.println("How old are you");
//Now the user enters the age
line = br.readLine();
System.out.println("Okey! So your input was:\n " + line );
}
}
You can get a String object contains the whole line that input from the user... Have Fun
Hi!
Thank you for the reply :)
But it doesnt seam to work. I have J2SE 5.0. When i run the program I get:
-jGRASP exec: java Test
java.lang.UnsupportedClassVersionError: Test (Unsupported major.minor version 49.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Exception in thread "main"
-jGRASP wedge2: exit code for process is 1.
-jGRASP: operation complete.
Thank you in advance :)
Yes you're right. It worked when I executet and compiled it with the dos-prompt.
I have one question. How do you do so that it reads in? I tried this but I get errors:
System.out.println("Okey! So your input was:\n " + 1+(int)line );
Line is a String (that represents an int). So you want to find a method for converting a String to an int. Go to the API docs and look at the Integer class. There is a static method that will do that for you. You might need to trim() your String first.
http://java.sun.com/j2se/1.4.2/docs/api/
String num = "123";
int n = Integer.parseInt( num ); //The String "123" is converted to the int 123
System.out.println( "This is my number: " + n );
Note: Be carefule when using this method, since the String u passed must represents an integral number, otherwise an exception is thrown... GoodLuck