我收到错误,说编译器找不到我的变量“complexArray”,但我不知道为什么。如何修复程序以便返回从文件中读取的复数数组?
public static Complex[] parseFromFile(String fileName) {
int numOfComplex = 0;
try {
Scanner sc = new Scanner(new File(fileName));
String firstLine = sc.nextLine();
firstLine = firstLine.trim();
numOfComplex = Integer.parseInt(firstLine);
Complex[] complexArray = new Complex[numOfComplex];
for (int i = 0; i < numOfComplex; i++) {
String nextLine = sc.nextLine();
nextLine = nextLine.trim();
complexArray[i] = parseComplex(nextLine);
}
}
catch(Exception e) {
}
return complexArray;
}
答案 0 :(得分:0)
您的complexArray变量在try {}范围内声明。在try语句之前声明它。
Complex [] complexArray = null;
答案 1 :(得分:0)
将tryArray语句放在try {}语句中
public static Complex[] parseFromFile(String fileName) {
int numOfComplex = 0;
try {
Scanner sc = new Scanner(new File(fileName));
String firstLine = sc.nextLine();
firstLine = firstLine.trim();
numOfComplex = Integer.parseInt(firstLine);
Complex[] complexArray = new Complex[numOfComplex];
for (int i = 0; i < numOfComplex; i++) {
String nextLine = sc.nextLine();
nextLine = nextLine.trim();
complexArray[i] = parseComplex(nextLine);
}
return complexArray;
}
catch(Exception e) {
}
}
答案 2 :(得分:0)
因为在complexArray
块中声明了try
,所以一旦try块内的执行完成,complexArray
就超出了范围。
public static Complex[] parseFromFile(String fileName) {
int numOfComplex = 0;
try {
Scanner sc = new Scanner(new File(fileName));
String firstLine = sc.nextLine();
firstLine = firstLine.trim();
numOfComplex = Integer.parseInt(firstLine);
Complex[] complexArray = new Complex[numOfComplex];
for (int i = 0; i < numOfComplex; i++) {
String nextLine = sc.nextLine();
nextLine = nextLine.trim();
complexArray[i] = parseComplex(nextLine);
}
} //<<<=== scope of `complexArray` ends here
catch(Exception e) {
}
return complexArray;
}
试试这个
public static Complex[] parseFromFile(String fileName) {
int numOfComplex = 0;
Complex[] complexArray;
try {
Scanner sc = new Scanner(new File(fileName));
String firstLine = sc.nextLine();
firstLine = firstLine.trim();
numOfComplex = Integer.parseInt(firstLine);
complexArray = new Complex[numOfComplex];
for (int i = 0; i < numOfComplex; i++) {
String nextLine = sc.nextLine();
nextLine = nextLine.trim();
complexArray[i] = parseComplex(nextLine);
}
}
catch(Exception e) {
}
return complexArray;
} //<<<=== scope of `complexArray` ends here
答案 3 :(得分:0)
从@hemanth继续
public static Complex[] parseFromFile(String fileName) {
int numOfComplex = 0;
Complex[] complexArray = new Complex[numOfComplex]; // Need to initialize the array
try {
Scanner sc = new Scanner(new File(fileName));
String firstLine = sc.nextLine();
firstLine = firstLine.trim();
numOfComplex = Integer.parseInt(firstLine);
complexArray = new Complex[numOfComplex];
for (int i = 0; i < numOfComplex; i++) {
String nextLine = sc.nextLine();
nextLine = nextLine.trim();
complexArray[i] = parseComplex(nextLine);
}
return complexArray;
}
catch(Exception e) {
System.out.println("Exception was thrown"); // try adding this to see if an exception is thrown.
}
return complexArray;
} //<<<=== scope of `complexArray` ends here
您需要通过创建新的Complex数组来初始化complexArray对象。这是你的问题。
我认为这可行。如果一切顺利,它应该进入try子句。如果不是,它将返回大小为0的complexArray。我假设如果发生这种情况,文件有问题,这意味着你需要手动做一些事情?