views:

2583

answers:

6
+2  A: 

The Scanner class is new in Java 5. I do not know what Hardy's default Java environment is, but it is not Sun's and therefore may be outdated.

I recommend installing the package sun-java6-jdk to get the most up-to-date version, then telling Eclipse to use it.

Thomas
A: 

If you are using a version of Java before 1.5, java.util.Scanner doesn't exist.

Which version of the JDK is your Eclipse project set up to use?

Have a look at Project, Properties, Java Build Path -- look for the 'JRE System Library' entry, which should have a version number next to it.

tdavies
A: 

It could also be that although you are have JDK 1.5 or higher, the project has some specific settings set that tell it to compile as 1.4. You can test this via Project >> Properties >> Java Compiler and ensure the "Compiler Compliance Level" is set to 1.5 or higher.

Lee Theobald
A: 

I had it set to 1.5 but for some reason that was not sufficient. I set it to the sun-java6-jdk and that worked out fine!

Austin
+1  A: 

The answer is there http://ubuntuforums.org/showthread.php?t=874500&page=2

A: 

I know, It's quite a while since the question was posted. But the solution may still be of interest to anyone out there. It's actually quite simple...

Under Ubuntu you need to set the java compiler "javac" to use sun's jdk instead of any other alternative. The difference to some of the answers posted so far is that I am talking about javac NOT java. To do so fire up a shell and do the following:

  1. As root or sudo type in at command line:

# update-alternatives --config javac

  1. Locate the number pointing to sun's jdk, type in this number, and hit "ENTER".

  2. You're done! From now on you can enjoy java.util.Scanner under Ubuntu.

System.out.println("Say thank you, Mr."); Scanner scanner = java.util.Scanner(System.in); String thanks = scanner.next(); System.out.println("Your welcome.");