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() ;
}
}
}
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:
PascalCaseIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With