简体   繁体   中英

Scanner cannot be resolved to a type

I just installed Ubuntu 8.04 and I'm taking a course in Java so I figured why not install a IDE while I am installing it. So I pick my IDE of choice, Eclipse, and I make a very simple program, Hello World, to make sure everything is running smoothly. When I go to use Scanner for user input I get a very odd error:

import java.util.Scanner;

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

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)

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.

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

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.

package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Input seconds: ");
        int num = in.nextInt();

        for (int i = 1; i <=num; i++) {

            if(i%10==3)
            {
                System.out.println(i);
            }
        }

    }
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM