英文:
Returning an object within a method containing try-catch block (Java)
问题
public static ReadCSVExample1 readBook(String filename, String serialNumber) {
try {
String line = "";
BufferedReader br = new BufferedReader(new FileReader(filename));
String foundName = "";
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
for (int z = 0; z < values.length; z++) {
if (values[z].equals(serialNumber)) {
System.out.println(values[z] + " found!");
foundName = values[z + 1];
System.out.println(values[z + 2]);
System.out.println(values[z + 3]);
}
}
}
ReadCSVExample1 r = new ReadCSVExample1(foundName);
return r;
} catch (FileNotFoundException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
}
ReadCSVExample1 r = new ReadCSVExample1(foundName);
return r;
}
英文:
I wanted to return an object within a method that has a try-catch block. As shown below the method takes in a CSV file, reads the serialNumber
and is supposed to return an object of the book with attributes such as name, year, etc., from the CSB file. I want to just return this object but I don't know where in the scope to return it given that foundName
is local and thus I won't be able to return the new object r
.
public static ReadCSVExample1 readBook(String filename, String serialNumber){
try{
String line = "";
BufferedReader br = new BufferedReader(new FileReader(filename));
String foundName = "";
while ((line = br.readLine()) != null){
String[] values = line.split(",");
for (int z = 0; z<values.length; z++){
if (values[z].equals(serialNumber)){
System.out.println(values[z]+ " found!");
foundName = values[z+1];
System.out.println(values[z+2]);
System.out.println(values[z+3]);
}
}
}
} catch (FileNotFoundException e){
System.err.println(e);
} catch (IOException e){
System.err.println(e);
}ReadCSVExample1 r = new ReadCSVExample1(foundName);
return r;
}
答案1
得分: 0
只需将foundName
从try-catch
块中移到外面,代码如下所示:
public static ReadCSVExample1 readBook(String filename, String serialNumber){
String foundName = "";
try{
String line = "";
BufferedReader br = new BufferedReader(new FileReader(filename));
while ((line = br.readLine()) != null){
String[] values = line.split(",");
for (int z = 0; z<values.length; z++){
if (values[z].equals(serialNumber)){
System.out.println(values[z]+ " found!");
foundName = values[z+1];
System.out.println(values[z+2]);
System.out.println(values[z+3]);
}
}
}
} catch (FileNotFoundException e){
System.err.println(e);
return null;
} catch (IOException e){
System.err.println(e);
return null;
}
ReadCSVExample1 r = new ReadCSVExample1(foundName);
return r;
}
如果不将其放在try-catch块之外,`foundName`的作用域将限于try-catch块内部。如果将其移到外部,其作用域将是整个方法。
英文:
Just move the foundName
outside the try-catch
block like so:
public static ReadCSVExample1 readBook(String filename, String serialNumber){
String foundName = "";
try{
String line = "";
BufferedReader br = new BufferedReader(new FileReader(filename));
while ((line = br.readLine()) != null){
String[] values = line.split(",");
for (int z = 0; z<values.length; z++){
if (values[z].equals(serialNumber)){
System.out.println(values[z]+ " found!");
foundName = values[z+1];
System.out.println(values[z+2]);
System.out.println(values[z+3]);
}
}
}
} catch (FileNotFoundException e){
System.err.println(e);
return null;
} catch (IOException e){
System.err.println(e);
return null;
}ReadCSVExample1 r = new ReadCSVExample1(foundName);
return r;
}
If it's not outside the try-catch block, the scope of foundName
is within the try-catch block. If you move it outside, it's scope is the entire method.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论