在包含try-catch块的方法中返回一个对象(Java)

huangapple go评论72阅读模式
英文:

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 = &quot;&quot;;
            BufferedReader br = new BufferedReader(new FileReader(filename));
            String foundName = &quot;&quot;;
            while ((line = br.readLine()) != null){
                String[] values = line.split(&quot;,&quot;);
                for (int z = 0; z&lt;values.length; z++){
                    if (values[z].equals(serialNumber)){
                        System.out.println(values[z]+ &quot; found!&quot;);
                        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

只需将foundNametry-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 = &quot;&quot;;
        try{
            String line = &quot;&quot;;
            BufferedReader br = new BufferedReader(new FileReader(filename));
            
            while ((line = br.readLine()) != null){
                String[] values = line.split(&quot;,&quot;);
                for (int z = 0; z&lt;values.length; z++){
                    if (values[z].equals(serialNumber)){
                        System.out.println(values[z]+ &quot; found!&quot;);
                        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.

huangapple
  • 本文由 发表于 2020年10月6日 05:51:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/64216694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定