Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving probelms with "try with resources"
    text
    copied!<p><strong>TO CLARIFY:</strong> I can't even compile due to messages from Eclipse. First code snippet: <code>input</code> and <code>inputBuffer</code> are not recognized. Second code snippet, Eclipse wants me to switch switch "Compliance and JRE to 1.7"</p> <p>I am new to try-with-resources and I can't quite understand the syntax or what I'm doing wrong. Here is my code</p> <pre><code>try { FileReader input = new FileReader(this.fileName); BufferedReader inputBuffer = new BufferedReader (input); String line; while ((line = inputBuffer.readLine()) != null) { String[] inputData = line.split(","); Node&lt;Integer&gt; newNode = new Node&lt;Integer&gt;(Integer.parseInt(inputData[0]), Integer.parseInt(inputData[1])); this.hashMap.add(newNode); } //inputBuffer.close(); //input.close(); }catch (NumberFormatException nfe){ System.out.println( "Repository could not load data due to NumberFormatException: " + nfe); }catch (FileNotFoundException fnfe) { System.out.println("File not found, error: " + fnfe); }finally { inputBuffer.close(); input.close(); } </code></pre> <p>The finally block does not work, so i wanted to try</p> <pre><code>try (FileReader input = new FileReader(this.fileName)) { ...... }catch (FileNotFoundException e) { ...... }finally { inputBuffer.close(); input.close(); } </code></pre> <p><strong>However</strong> </p> <ul> <li><p>I should also add BufferedReader to <code>try (...)</code> ... but how ?</p></li> <li><p>Also this requires me to switch "Compliance and JRE to 1.7". I don't know what that means and how that would affect my program so far, I'm not willing to do it until someone explains what it all means or if I'm doing something wrong.</p></li> </ul> <p><strong>EDIT</strong></p> <p>I moved declaration before try block and initialized with null, is this "ok" ?</p> <pre><code>FileReader input = null; BufferedReader inputBuffer = null; try { input = new FileReader(this.fileName); inputBuffer = new BufferedReader (input); ... } ... </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload