Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot be resolved to a type" when attempting to use Scanner

when i run following code it shows error that scanner cannot be resolved to type. i checked that jre is installed and version is 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() ;
}
}
}
like image 507
user4036695 Avatar asked Oct 22 '25 05:10

user4036695


1 Answers

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

import java.util.Scanner;

In addition, I'd like to pose a few more possible corrections:

  • Class names should be PascalCase
  • Your code should have consistent indentation. Ctrl+Shift+F is your friend here.
like image 104
nanofarad Avatar answered Oct 23 '25 19:10

nanofarad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!