Simple Input

Hi!How do you read an input? In my school we use this keyboard class http://www.gleerups.se/skola/pager.asp?sid=207But since this is Java there must be another easier way ;-)Thanx in advance
[218 byte] By [Danii] at [2007-9-30 22:07:04]
# 1

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)

Zebediah at 2007-7-7 11:20:12 > top of Java-index,Administration Tools,Sun Connection...
# 2

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 :)

Danii at 2007-7-7 11:20:12 > top of Java-index,Administration Tools,Sun Connection...
# 3

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

MohdSleem at 2007-7-7 11:20:12 > top of Java-index,Administration Tools,Sun Connection...
# 4

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 :)

Danii at 2007-7-7 11:20:12 > top of Java-index,Administration Tools,Sun Connection...
# 5
It looks like your jGrasp is using a version of java older than 5.0. The unsupported version error is usually caused by an older JVM trying to use a new class file. I am not familiar with jGrasp so I don't know how to help you fix that.
atmguy at 2007-7-7 11:20:12 > top of Java-index,Administration Tools,Sun Connection...
# 6

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 );

Danii at 2007-7-7 11:20:12 > top of Java-index,Administration Tools,Sun Connection...
# 7

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/

atmguy at 2007-7-7 11:20:12 > top of Java-index,Administration Tools,Sun Connection...
# 8

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

MohdSleem at 2007-7-7 11:20:12 > top of Java-index,Administration Tools,Sun Connection...