简体   繁体   English

尝试使用扫描仪时“无法解析为类型”

[英]"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?我检查了 jre 是否已安装并且版本是 1.7 我还需要检查什么? 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:在文件的最顶部,在public class student之上,您需要添加:

import java.util.Scanner;

In addition, I'd like to pose a few more possible corrections:此外,我想提出更多可能的更正:

  • Class names should be PascalCase类名应该是PascalCase
  • Your code should have consistent indentation.您的代码应该具有一致的缩进。 Ctrl+Shift+F is your friend here. Ctrl+Shift+F 是你的朋友。

Just use import java.util.Scanner;只需使用import java.util.Scanner; or use import java.util.*;或使用import java.util.*;

I tried the code myself and it works.我自己尝试了代码并且它有效。 Therefore, it is a configuration problem.因此,这是一个配置问题。 Since you tried to import java.util.Scanner, as hexafraction suggested, then I suppose the JRE is not properly configured.由于您尝试导入 java.util.Scanner,正如 hexafraction 所建议的那样,那么我想 JRE 没有正确配置。

Try :尝试 :

  • Right clicking your project name -> Click properties -> Click Java Build Path右键单击您的项目名称 -> 单击属性 -> 单击 Java 构建路径
  • Select the libraries tab选择库选项卡
  • Click on add class folder (at the right) then select your Class.单击添加班级文件夹(在右侧),然后选择您的班级。

Edit : Even tho it would not really solve the problem, copy the src folder in a new project would probably solve your issue.编辑:即使它不能真正解决问题,在新项目中复制 src 文件夹也可能会解决您的问题。

Check your code's compilation level by right click your project in eclipse and click properties.通过在 Eclipse 中右键单击您的项目并单击属性来检查您的代码的编译级别。

It might point to 1.6 or lower.它可能指向 1.6 或更低。 If it is the case point it to 1.7如果是这种情况,请将其指向 1.7

This might solve your problem.这可能会解决您的问题。

I hope it helps.我希望它有帮助。

在此处输入图片说明

Right click on your package > Navigate to properties > Click on java build path > Click on libraries tab > Click on add library > Select JRE System library > Click on next > Click on Finish > Click on apply and Close.右键单击您的包 > 导航到属性 > 单击 java 构建路径 > 单击库选项卡 > 单击添加库 > 选择 JRE 系统库 > 单击下一步 > 单击完成 > 单击应用并关闭。 enter image description here It should work.... I hope so :)在此处输入图像描述它应该可以工作......我希望如此:)

Just type sc.close();只需输入sc.close(); just after the last third curly bracket } to close the scanner at the end...your issue will be resolved!在最后一个第三个大括号}之后关闭扫描仪...您的问题将得到解决!

Keep Messing with the options you get in eclipse , for example like try block for scanner and so on.. it will fix itself after a while.继续搞乱你在 eclipse 中得到的选项,例如像 try 块用于扫描仪等等......它会在一段时间后自行修复。 I don't know why this is happening.我不知道为什么会这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM