Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first version is <em>nearly</em> right - but there are two problems:</p> <ol> <li><p>Your method doesn't declare that it can throw <code>IOException</code>, but your use of <code>valueFile.seek</code> could throw an exception. Either you need to change the method declaration, or catch and handle the exception.</p></li> <li><p>The compiler doesn't know that <code>System.exit(0)</code> will never return normally - it thinks it's a normal method. So you have to change your catch block to end with something like:</p> <pre><code>return; </code></pre> <p>or</p> <pre><code>throw new AssertionError("We'll never get here."); </code></pre> <p>At that point, the compiler knows that if it gets beyond the try/catch block, <code>valueFile</code> <em>is</em> definitely assigned.</p></li> </ol> <p>Note that if you move <em>everything</em> inside the <code>catch</code> block, both problems will go away - but I'd suggest that you'd be better off <em>not</em> catching anything at all, and instead just declaring that the method may throw <code>IOException</code>:</p> <pre><code>public void foo(int iterations) throws IOException { RandomAccessFile valueFile = new RandomAccessFile("valuefile.txt", "rw"); for (int i = 0; i &lt; iterations; i++) { valueFile.writeDouble(randomizer.nextDouble()*200); } for (int i = 0; i&lt; iterations; i++) { double total = 0; valueFile.seek(0); // Presumably you'd read the contents here? } } </code></pre> <p>... except you should <em>also</em> close the file when you're done with it, using a try-with-resources statement if you're using Java 7, or just a try/finally block otherwise.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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