javaeclipsejava.util.scanner

How to solve "The method nextInt() is undefined for the type Scanner"?


I have a problem with java util Scanner and i can't find the error. I need to use Scanner to compare different int. I'm using Eclipse IDE. This is the code:

package TercerCas;
import java.util.Scanner;
public class PrimeraActivitat {
public static void main(String[] args) {

int nombre1;
int nombre2;
int nombre3;

Scanner ent = new Scanner(System.in);

nombre1= ent.nextInt();
nombre2= ent.nextInt();
nombre3= ent.nextInt();

if (nombre1>nombre2) {
if (nombre1>nombre3) {
System.out.println(nombre1);
}else {
System.out.println(nombre3);
}
}else if (nombre2>nombre3) {
System.out.println(nombre3);
}
ent.close();
}
}

And I get the following errors:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The constructor Scanner(InputStream) is undefined
The method nextInt() is undefined for the type Scanner
The method nextInt() is undefined for the type Scanner
The method nextInt() is undefined for the type Scanner
The method close() is undefined for the type Scanner

at TercerCas.PrimeraActivitat.main(PrimeraActivitat.java:15)

Solution

  • The method nextInt() is undefined for the type Scanner, the type Scanner must be imported from java.util.Scanner. If you have any other Class with Name Scanner in the Same package, this error may be possible at compilation time.

    Try using the fully qualified name for Scanner class like java.util.Scanner ent = new java.util.Scanner(System.in);