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

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

Returning an object within a method containing try-catch block (Java)

问题

  1. public static ReadCSVExample1 readBook(String filename, String serialNumber) {
  2. try {
  3. String line = "";
  4. BufferedReader br = new BufferedReader(new FileReader(filename));
  5. String foundName = "";
  6. while ((line = br.readLine()) != null) {
  7. String[] values = line.split(",");
  8. for (int z = 0; z < values.length; z++) {
  9. if (values[z].equals(serialNumber)) {
  10. System.out.println(values[z] + " found!");
  11. foundName = values[z + 1];
  12. System.out.println(values[z + 2]);
  13. System.out.println(values[z + 3]);
  14. }
  15. }
  16. }
  17. ReadCSVExample1 r = new ReadCSVExample1(foundName);
  18. return r;
  19. } catch (FileNotFoundException e) {
  20. System.err.println(e);
  21. } catch (IOException e) {
  22. System.err.println(e);
  23. }
  24. ReadCSVExample1 r = new ReadCSVExample1(foundName);
  25. return r;
  26. }
英文:

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.

  1. public static ReadCSVExample1 readBook(String filename, String serialNumber){
  2. try{
  3. String line = &quot;&quot;;
  4. BufferedReader br = new BufferedReader(new FileReader(filename));
  5. String foundName = &quot;&quot;;
  6. while ((line = br.readLine()) != null){
  7. String[] values = line.split(&quot;,&quot;);
  8. for (int z = 0; z&lt;values.length; z++){
  9. if (values[z].equals(serialNumber)){
  10. System.out.println(values[z]+ &quot; found!&quot;);
  11. foundName = values[z+1];
  12. System.out.println(values[z+2]);
  13. System.out.println(values[z+3]);
  14. }
  15. }
  16. }
  17. } catch (FileNotFoundException e){
  18. System.err.println(e);
  19. } catch (IOException e){
  20. System.err.println(e);
  21. }ReadCSVExample1 r = new ReadCSVExample1(foundName);
  22. return r;
  23. }

答案1

得分: 0

只需将foundNametry-catch块中移到外面,代码如下所示:

  1. public static ReadCSVExample1 readBook(String filename, String serialNumber){
  2. String foundName = "";
  3. try{
  4. String line = "";
  5. BufferedReader br = new BufferedReader(new FileReader(filename));
  6. while ((line = br.readLine()) != null){
  7. String[] values = line.split(",");
  8. for (int z = 0; z<values.length; z++){
  9. if (values[z].equals(serialNumber)){
  10. System.out.println(values[z]+ " found!");
  11. foundName = values[z+1];
  12. System.out.println(values[z+2]);
  13. System.out.println(values[z+3]);
  14. }
  15. }
  16. }
  17. } catch (FileNotFoundException e){
  18. System.err.println(e);
  19. return null;
  20. } catch (IOException e){
  21. System.err.println(e);
  22. return null;
  23. }
  24. ReadCSVExample1 r = new ReadCSVExample1(foundName);
  25. return r;
  26. }
  27. 如果不将其放在try-catch块之外`foundName`的作用域将限于try-catch块内部如果将其移到外部其作用域将是整个方法
英文:

Just move the foundName outside the try-catch block like so:

  1. public static ReadCSVExample1 readBook(String filename, String serialNumber){
  2. String foundName = &quot;&quot;;
  3. try{
  4. String line = &quot;&quot;;
  5. BufferedReader br = new BufferedReader(new FileReader(filename));
  6. while ((line = br.readLine()) != null){
  7. String[] values = line.split(&quot;,&quot;);
  8. for (int z = 0; z&lt;values.length; z++){
  9. if (values[z].equals(serialNumber)){
  10. System.out.println(values[z]+ &quot; found!&quot;);
  11. foundName = values[z+1];
  12. System.out.println(values[z+2]);
  13. System.out.println(values[z+3]);
  14. }
  15. }
  16. }
  17. } catch (FileNotFoundException e){
  18. System.err.println(e);
  19. return null;
  20. } catch (IOException e){
  21. System.err.println(e);
  22. return null;
  23. }ReadCSVExample1 r = new ReadCSVExample1(foundName);
  24. return r;
  25. }

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:

确定