Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If the error you’re getting is something like this:</p> <pre><code>Foo.java:23: error: variable answer might not have been initialized System.out.print("answer is " + Integer.toString(answer)); </code></pre> <p>then the problem is that <code>answer</code> might not have had any value set, and the computer gives up and prints an error message instead of possibly returning garbage.</p> <p>How can <code>answer</code> not have a value set? You set <code>answer</code> to <code>x+y</code> if <code>x&gt;y</code>, and if <code>x&lt;y</code> you set <code>answer=x-y</code>; but what if <code>x=y</code>? There’s nothing that tells the computer what to do in that case. Even though <code>x</code> might not be equal to <code>y</code> for the particular inputs you typed into your program, Java gives you an error message now in the hopes that you would rather get an error now, when it can easily be fixed, than sometime much later when someone first inputs equal values of <code>x</code> and <code>y</code>.</p> <p>You can fix this by changing <code>x&gt;y</code> to <code>x&gt;=y</code>, or <code>x&lt;y</code> to <code>x&lt;=y</code>, or by adding a third <code>x==y</code> case.</p> <hr> <p>If the warning you are getting is</p> <pre><code>Warning: The local variable answer is never read </code></pre> <p>Then you need to add a statement that uses <code>answer</code> in some way, for example by adding this at the end of the method:</p> <pre><code>System.out.print("answer is " + Integer.toString(answer)); </code></pre> <p>However, note that this is just a <em>warning</em>, not an <em>error</em>, so even though the message pops up, it should run just fine—try it! Of course, you won’t be able to see what value <code>answer</code> gets, because, as the warning says, <code>answer</code> is never read in the current code.</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. 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