【发布时间】:2021-08-31 18:52:47
【问题描述】:
我正在运行此代码并收到错误
System.out.print(name[i]+" ");
System.out.print(age[i]+" ");
System.out.print(country[i]+" ");
那个
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
name cannot be resolved to a variable
age cannot be resolved to a variable
country cannot be resolved to a variable
我该如何解决这个问题?
public class AnyInteger
{
public static void main(String arg[]) throws FileNotFoundException
{
int n = 0;
try {
BufferedReader in = new BufferedReader(new FileReader("D:\\Java\\eclipse-workspace\\AnyInteger\\src\\file.txt"));
String str;
while ((str = in.readLine())!= null) {
String[] arr =str.split("#");
n = arr.length;
String name[] = new String[n];
int age[] = new int[n];
String country[] = new String[n];
int i=0;
while(i<n)
{
for(int j=0;j<3;j++)
{
name[j] = arr[i];
i = i+1;
age[j] = Integer.parseInt(arr[i]);
i = i+1;
country[j] = arr[i];
i = i+1;
}
}
}
in.close();
}
catch (IOException e) {
System.out.println("File Read Error");
}
System.out.println("Names: ");
for(int i=0;i<n;i++)
{
System.out.print(name[i]+" ");
}
System.out.println("Ages: ");
for(int i=0;i<n;i++)
{
System.out.print(age[i]+" ");
}
System.out.println("Countries: ");
for(int i=0;i<n;i++)
{
System.out.print(country[i]+" ");
}
}
}
【问题讨论】:
-
names数组是while循环的局部变量,不能在while循环外使用
-
同样
age和country。 -
那我该怎么改,请指教
-
显示输入示例