Return 数组在 try/catch 内部初始化
Return array initialized inside try/catch
我正在创建一个程序,该程序将一个文件读取到一个数组中,将哪个文件分离到数组中的不同索引值中。
static String[] readFile () {
int count = 0;
try {
File file = new File("input.txt"); // create file
Scanner scanner = new Scanner(file); // create scanner associated to file
// counts number of lines
while (scanner.hasNextLine()) {
scanner.nextLine();
count++;
}
// reads file into array
while (scanner.hasNextLine()) {
String[] data = new String[count];
int len = data.length;
for (int i = 0; i <= len; i++) {
data[i] = scanner.nextLine();
}
}
} catch (Exception e) {
System.out.println("File not found!!!");
System.exit(0);
}
return data;
}
问题是,当尝试 return 变量数据时,我收到一条错误消息,提示“无法解析符号数据”,因为它是在 try-catch 块中初始化的。我试过 this 但它 return 的值为空,因为变量的长度由变量 count 决定,而变量 count 的值也在 catch 块中确定。提前致谢!
您可以使用来自评论的@Sweeper 建议。它看起来像这样。
static ArrayList<String> readFile () {
ArrayList<String> data = new ArrayList<>();
try {
File file = new File("input.txt"); // create file
Scanner scanner = new Scanner(file); // create scanner associated to file
while (scanner.hasNextLine()) {
data.add(scanner.nextLine()) ;
}
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
但是如果您想保留当前代码,您可以在 try 块中通过 null 初始化 data
。而且您还需要重置扫描仪。您的代码看起来像这样。请注意,在 for
循环中,您必须使用条件 <len
而不是 <=len
.
static String[] readFile () {
String[] data = null;
try {
File file = new File("input.txt"); // create file
Scanner scanner = new Scanner(file); // create scanner associated to file
// counts number of lines
int count = 0;
while (scanner.hasNextLine()) {
scanner.nextLine();
count++;
}
scanner.close(); // don't forget about closing resources
data = new String[count];
// reads file into array
scanner = new Scanner(file);
for (int i = 0; i < len; i++) {
data[i] = scanner.nextLine();
}
scanner.close();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
您首先遇到了 2 个问题,因为变量数据是在 try-catch 块中声明的——如果指令抛出异常并且变量数据从未被声明怎么办?在这种情况下,要返回什么?,解决方案是在 try-catch 块之前声明变量数据,第二个发生是因为当您调用 nextLine() 时,Scanner 对象会保持其状态,因此当您尝试调用 nextLine 时() 在遍历整个文件后再次出现在最后一行(没有下一行),您可以调用 close() 解决它,然后再次初始化扫描仪对象,这将重置状态:
static String[] readFile () {
Scanner scanner = null;
int count = 0;
String[] data = null;
try {
File file = new File("C:\Users\Mulé\Desktop\doc.txt"); // create file
scanner = new Scanner(file); // create scanner associated to file
// counts number of lines
while (scanner.hasNextLine()) {
scanner.nextLine();
count++;
}
scanner.close();
scanner = new Scanner(file);
// reads file into array
data = new String[count];
for (int i = 0; i < data.length; i++) {
data[i] = scanner.nextLine();
}
} catch (Exception e) {
System.out.println("File not found!!!");
System.exit(0);
}
return data;
}
以下是一些类似的问题及其答案:
Java: Reading a file into an array
Read text file into an array
此外,我想指出 try-with-resources 语句 - Scanner
对象应该在其中关闭或初始化。
此外,System.exit(0);
不是停止方法的好方法,因为在这种情况下它周围的所有 finally
块都不会被执行。
我正在创建一个程序,该程序将一个文件读取到一个数组中,将哪个文件分离到数组中的不同索引值中。
static String[] readFile () {
int count = 0;
try {
File file = new File("input.txt"); // create file
Scanner scanner = new Scanner(file); // create scanner associated to file
// counts number of lines
while (scanner.hasNextLine()) {
scanner.nextLine();
count++;
}
// reads file into array
while (scanner.hasNextLine()) {
String[] data = new String[count];
int len = data.length;
for (int i = 0; i <= len; i++) {
data[i] = scanner.nextLine();
}
}
} catch (Exception e) {
System.out.println("File not found!!!");
System.exit(0);
}
return data;
}
问题是,当尝试 return 变量数据时,我收到一条错误消息,提示“无法解析符号数据”,因为它是在 try-catch 块中初始化的。我试过 this 但它 return 的值为空,因为变量的长度由变量 count 决定,而变量 count 的值也在 catch 块中确定。提前致谢!
您可以使用来自评论的@Sweeper 建议。它看起来像这样。
static ArrayList<String> readFile () {
ArrayList<String> data = new ArrayList<>();
try {
File file = new File("input.txt"); // create file
Scanner scanner = new Scanner(file); // create scanner associated to file
while (scanner.hasNextLine()) {
data.add(scanner.nextLine()) ;
}
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
但是如果您想保留当前代码,您可以在 try 块中通过 null 初始化 data
。而且您还需要重置扫描仪。您的代码看起来像这样。请注意,在 for
循环中,您必须使用条件 <len
而不是 <=len
.
static String[] readFile () {
String[] data = null;
try {
File file = new File("input.txt"); // create file
Scanner scanner = new Scanner(file); // create scanner associated to file
// counts number of lines
int count = 0;
while (scanner.hasNextLine()) {
scanner.nextLine();
count++;
}
scanner.close(); // don't forget about closing resources
data = new String[count];
// reads file into array
scanner = new Scanner(file);
for (int i = 0; i < len; i++) {
data[i] = scanner.nextLine();
}
scanner.close();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
您首先遇到了 2 个问题,因为变量数据是在 try-catch 块中声明的——如果指令抛出异常并且变量数据从未被声明怎么办?在这种情况下,要返回什么?,解决方案是在 try-catch 块之前声明变量数据,第二个发生是因为当您调用 nextLine() 时,Scanner 对象会保持其状态,因此当您尝试调用 nextLine 时() 在遍历整个文件后再次出现在最后一行(没有下一行),您可以调用 close() 解决它,然后再次初始化扫描仪对象,这将重置状态:
static String[] readFile () {
Scanner scanner = null;
int count = 0;
String[] data = null;
try {
File file = new File("C:\Users\Mulé\Desktop\doc.txt"); // create file
scanner = new Scanner(file); // create scanner associated to file
// counts number of lines
while (scanner.hasNextLine()) {
scanner.nextLine();
count++;
}
scanner.close();
scanner = new Scanner(file);
// reads file into array
data = new String[count];
for (int i = 0; i < data.length; i++) {
data[i] = scanner.nextLine();
}
} catch (Exception e) {
System.out.println("File not found!!!");
System.exit(0);
}
return data;
}
以下是一些类似的问题及其答案:
Java: Reading a file into an array
Read text file into an array
此外,我想指出 try-with-resources 语句 - Scanner
对象应该在其中关闭或初始化。
此外,System.exit(0);
不是停止方法的好方法,因为在这种情况下它周围的所有 finally
块都不会被执行。