Austin
Austin

Reputation: 153

Scanner cannot be resolved to a type

I just installed Ubuntu 8.04 and Eclipse.

I made a very simple Hello World program in order to ensure that everything is running smoothly. When I attempted to use the Scanner class for acquiring some user input I got a very odd error.

My code:

import java.util.Scanner;

class Test {
    public static void main (String [] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("hi");
    }
}

Error message

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Scanner cannot be resolved to a type
    Scanner cannot be resolved to a type

   at test.main(test.java:5)

Upvotes: 0

Views: 27677

Answers (5)

h3lium
h3lium

Reputation:

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 can now use java.util.Scanner under Ubuntu.

Upvotes: 0

boi yeet
boi yeet

Reputation: 86

You imported Scanner but you're not using it. You're using Scanner, which requires user inputs. You're trying to print out one thing, but you're exposing the your program to the fact that you are going to use your own input, so it decides to print "Hello World" after you give a user input. But since you are not deciding what the program will print, the system gets confused since it doesn't know what to print. You need something like int a=sc.nextInt(); or String b=sc.nextLine(); and then give your user input. But you said you want Hello World!, so Scanner is redundant.

Upvotes: -1

Lee Theobald
Lee Theobald

Reputation: 8587

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.

Upvotes: 0

tgdavies
tgdavies

Reputation: 11431

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.

Upvotes: 0

Thomas
Thomas

Reputation: 181855

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.

Upvotes: 5

Related Questions