"Unable to resolve type" when trying to use the scanner

when I run the following code, it shows an error that the scanner cannot resolve for input. i have checked that jre is installed and version 1.7, what else do i need to check? please, help.

public class student { 

String name;
int rollno;
public void get(String nm, int rno) 
{ name=nm;
rollno=rno;
}
public void  display()
{  System.out.println("Name of student is :" +name);
System.out.println("Roll no of student is :" +rollno);
}  
public static void main(String args[])
{ 
int i ;
int r1;
String n1;
student obj[]= new student[10];
Scanner sc=new Scanner(System.in);
for(i=0;i<10;i++)
{
obj[i]= new student();
}

for(i=0;i<10; i++)
{  System.out.println("Enter name:");
n1=sc.next();
sc.nextLine();
System.out.println("Enter roll no :");
r1=sc.nextInt();


obj[i].get(n1,r1) ;
obj[i].display() ;
}
}
}

      

+3


source to share


4 answers


You also need to import the class itself. At the very top of the file, above public class student

, you need to add:

import java.util.Scanner;

      



Also, I would like to make some more possible fixes:

  • Class names should be PascalCase

  • Your code should be consistently indented. Ctrl + Shift + F is your friend here.
+6


source


I tried the code myself and it works. Hence, this is a configuration issue. Since you were trying to import java.util.Scanner as suggested in hexafraction, I assume the JRE is not configured correctly.

Try:



  • Right Click on your project name -> Click Properties -> Click Java Build Path
  • Select the Libraries tab
  • Click to add a class folder (on the right), then select your class.

Edit: Even if that doesn't fix the problem, copy the src folder to a new project, maybe solve your problem.

+1


source


Just use import java.util.Scanner;

or useimport java.util.*;

+1


source


Check your code compilation level by right clicking the project in eclipse and clicking properties.

It can point to 1.6 or lower. If so, then it is 1.7

This might solve your problem.

Hope this helps.

enter image description here

0


source







All Articles