报cannot resolve symbol怎么处理

cqin 2003-03-17 05:42:53
IOTest.java:19: cannot resolve symbol
symbol : variable in
location: class IOTest
myline = in.readLine();
^
IOTest.java:23: cannot resolve symbol
symbol : variable in
location: class IOTest
myline = in.readLine();
^
2 errors


代码如下:
import java.io.*;
public class IOTest
{

public static void main(String args[]) // thrown FileNotFoundException
{
String myline;
try
{
BufferedReader in = new BufferedReader(new FileReader("Hello.java"));
}
catch(FileNotFoundException e)
{
System.out.println("Got one file exception");
e.printStackTrace();
}
try
{
myline = in.readLine();
while (myline != null)
{
System.out.println(myline);
myline = in.readLine();
}
}
catch (IOException e2)
{
System.out.println("Got one readline Exception");
e2.printStackTrace();
}
}
}
...全文
745 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
weimenren 2003-03-17
  • 打赏
  • 举报
回复
import java.io.*;
public class IOTest
{

public static void main(String args[]) // thrown FileNotFoundException
{
String myline;

try
{
BufferedReader in = new BufferedReader(new FileReader("Hello.java"));
myline = in.readLine();
while (myline != null)
{
System.out.println(myline);
myline = in.readLine();
}
}
catch(FileNotFoundException e)
{
System.out.println("Got one file exception");
e.printStackTrace();
}
catch (IOException e2)
{
System.out.println("Got one readline Exception");
e2.printStackTrace();
}
}
}


以上是改好的程序,cannot resolve symbol 的意思一般是变量没有定义,或者方法没有定义的意思,你原先的程序in在try{}中定义的,那么它的作用域就在{}中,外,假如你在try{}外定义的话,第二个myline = in.readLine();会怀疑你没有初始化in,而要求你初始化,所以最好把这些语句放在一起,当然可以分别捕获异常



ppxstar 2003-03-17
  • 打赏
  • 举报
回复
try
{
BufferedReader in = new BufferedReader(new FileReader("Hello.java"));
}

这一句,让你的变量in成了局部变量,使用范围只能在try{}里面
你应当这样写。
BufferedReader in;
try
{
in = new BufferedReader(new FileReader("Hello.java"));
}

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧