Scanner input error

I want to make a very simple program that allows the user to enter a key/character and have the corresponding unicode value returned. The problem is that I receive an error after the input of any character.

Here is the code:

import java.util.Scanner;

public class KeyMap

{

public static void main (String args[])

{

int number; //the future unicode value.

System.out.print ("Enter the key you want the unicode number of: ");

Scanner scan = new Scanner (System.in);

number = scan.nextInt();

char key = (char)number;

System.out.print ("The number for key " + key + " is " + number);

}

}

And I get this error in the run log after the character is entered:

Exception in thread "main" java.util.InputMismatchException

at java.util.Scanner.throwFor(Scanner.java:819)

at java.util.Scanner.next(Scanner.java:1431)

at java.util.Scanner.nextInt(Scanner.java:2040)

at java.util.Scanner.nextInt(Scanner.java:2000)

at KeyMap.main(KeyMap.java:11)

KeyMap has exited with status 1.

Thanks for any help!

[1133 byte] By [Templara] at [2007-10-3 3:30:52]
# 1

> Exception in thread "main"

> java.util.InputMismatchException

> at java.util.Scanner.throwFor(Scanner.java:819)

> at java.util.Scanner.next(Scanner.java:1431)

> at java.util.Scanner.nextInt(Scanner.java:2040)

> at java.util.Scanner.nextInt(Scanner.java:2000)

> at KeyMap.main(KeyMap.java:11)

Well that's coz your listening for an Integer input.

the error should happen if you input non-integer data type.

Nywled

Redxxiva at 2007-7-14 21:24:50 > top of Java-index,Java Essentials,New To Java...
# 2

import java.util.Scanner;

public class Test

{

public static void main(String args[])

{

int number; //the future unicode value.

System.out.print("Enter the key you want the unicode number of: ");

Scanner scan = new Scanner(System.in);

String str = scan.next();

char key = str.charAt(0);

number = (int)key;

System.out.println("The number for key " + key + " is " + number);

}

}

uncle_alicea at 2007-7-14 21:24:50 > top of Java-index,Java Essentials,New To Java...
# 3
Thanks! I just started trying to learn Java so there's a lot I need to get used to.
Templara at 2007-7-14 21:24:50 > top of Java-index,Java Essentials,New To Java...