3

This is my current code which is inside a javatogo package and a class file named Console.java.

package javatogo;
import java.util.Scanner;

public class Console {

    /**
     * Private static Scanner for multiple use.
     */
    
    // update: I did have System.in - I just did not copy and paste
    // the code directly from my project since it has things not needed
    private static Scanner scanner = new Scanner(System.in);
    
    /**
     * Static method to return a user input.
     * 
     * @param line
     * @return String
     */
    
    public static String readLine(
            String line
    ) {
        System.out.print(line);
        return scanner.nextLine();
    }

}

I found the documentation to read a console input which uses the java.util package and the Scanner.java however when I hit CTRL+SHIFT+O to automatically import or manually import it, I get an error on the import and instance:

Scanner cannot be resolved to a type

The import java.util.Scanner cannot be resolved

Any help would be appreciated, I have refreshed Eclipse and restarted it - I am still receiving this error.

Note: When I change the import to import java.util.* I receive no further errors on the import but the Scanner instance still errors.

Update: I took a print screen if it helps anyone.

Resolved because I updated my Java, I had to manually tell Eclipse my JDK path.
Thanks to @Pshemo for the answer and everyone who helped.

Community
  • 1
  • 1
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • I don't know Eclipse, but others IDE can't find system classes when the project JDK is not defined. Try deleting the project and maybe event reinstalling Eclipse – Raffaele Feb 03 '17 at 21:51
  • What if you use the fully qualified name? `java.util.Scanner scanner = new java.util.Scanner()` – Jorn Vernee Feb 03 '17 at 21:51
  • I still receive the error, I took a [print screen](http://imgur.com/a/QXPSi) if it helps anyone understand. @JornVernee – Jaquarh Feb 03 '17 at 21:52
  • Gonna guess that your project setup isn't right. You probably need to tell it where your Java installation lives. – nasukkin Feb 03 '17 at 21:54
  • Strange because it was only this morning I was working on a different project using the `Scanner` and it worked fine, I just loaded it and now it is erroring also. I will follow your advise and re install the Eclipse if deleting the project and making another doesn't work @Raffaele – Jaquarh Feb 03 '17 at 21:55
  • Try Project -> Clean... -> Clean all projects. It may also help to start Eclipse in clean mode as described here: http://stackoverflow.com/questions/2030064/how-to-run-eclipse-in-clean-mode-and-what-happens-if-we-do-so – Philipp Merkle Feb 03 '17 at 22:04
  • 1
    Error suggests that your project can't find location of proper JRE/JDK. If it worked earlier then change current JDK/JRE used in your project to alternative one which worked in different projects. Take a look here to see how to do it: http://stackoverflow.com/a/16919015/1393766 – Pshemo Feb 03 '17 at 22:05
  • That fixed the issue, if you change this to an answer I will accept it - thank-you so much! Maybe its because I updated my Java not long ago (I should of probably stated that but it didn't play on my mind - apologies) @Pshemo – Jaquarh Feb 03 '17 at 22:06
  • java.util.Scanner was first added to jdk in 1.5. So if you are using a java version older than that, you might get that error. – VHS Feb 03 '17 at 22:08

0 Answers0