Note that there are some explanatory texts on larger screens.

plurals
  1. POLearning to debug in Java
    primarykey
    data
    text
    <p>I'm both learning to use the JPDA on Netbeans and solving the <a href="http://www.spoj.pl/problems/PRIME1/" rel="nofollow noreferrer">Prime Generator</a> problem of Sphere's Online Judge. </p> <p>I've been reading <a href="http://www.netbeans.org/kb/55/using-netbeans/debug.html" rel="nofollow noreferrer">this tutorial on netbeans.org</a> about he JPDA, but haven't found it of much help. </p> <p>This code, which is based on a Sieve of Eratostenes implementation provided by starblue <a href="https://stackoverflow.com/questions/1042902/most-elegant-way-to-generate-prime-numbers">here</a>, is running like this:</p> <pre><code>2 1 10 //here the primes between 1 and 10 should print 3 5 //here the primes between 3 and 5 should print package sphere; /** * * @author Administrator */ //import java.util.ArrayList; import java.util.BitSet; import java.lang.Math.*; import java.util.ArrayList; public class Main { public static int ApproximateNthPrime(int nn) { double n = (double)nn; double p; if (nn &gt;= 7022) { p = n * Math.log(n) + n * (Math.log(Math.log(n)) - 0.9385); } else if (nn &gt;= 6) { p = n * Math.log(n) + n * Math.log(Math.log(n)); } else if (nn &gt; 0) { p = new int[] { 2, 3, 5, 7, 11 }[nn - 1]; } else { p = 0; } return (int)p; } // Find all primes up to and including the limit public static BitSet SieveOfEratosthenes(int limit) { final BitSet primes = new BitSet(); primes.set(0,false); primes.set(1,false); primes.set(2,limit,true); for (int i =0; i*i&lt;limit;i++) { if (primes.get(i)) { for (int j=i*1; j&lt;limit;j+=1) { primes.clear(j);// hace que el indice j sea false (no primo) } } } return primes; } public static ArrayList&lt;Integer&gt; GeneratePrimesSieveOfEratosthenes(int n) { int limit = ApproximateNthPrime(n); BitSet bits = SieveOfEratosthenes(limit); ArrayList &lt;Integer&gt; primes = new ArrayList&lt;Integer&gt;(); for (int i = 0, found = 0; i &lt; limit &amp;&amp; found &lt; n; i++) { if (bits.get(i)) { primes.add(i); found++; } } return primes; } public static void main (String[] args) throws java.lang.Exception { java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in)); String s; s= r.readLine(); int test_cases = Integer.parseInt(s); int case_counter =0; while (case_counter&lt;test_cases) { // System.out.println(s); s = r.readLine(); String [] splitted = s.split(" "); int lower_bound = Integer.parseInt(splitted[0]); int upper_bound = Integer.parseInt(splitted[1]); ArrayList &lt;Integer&gt; primesList= GeneratePrimesSieveOfEratosthenes(upper_bound); for (int i =0; i&lt;primesList.size();i++){ if (primesList.get(i)&lt;=lower_bound)System.out.println(primesList.get(i)); } case_counter++; System.out.println(" "); // space that separates test cases } } } </code></pre> <p>I know that the ArrayList primesList isn't getting initialized and I'm suspicious of this bit of code, cause honestly, I don't quite understand it:</p> <pre><code>if (primes.get(i)) { for (int j=i*1; j&lt;limit;j+=1) { primes.clear(j); } } </code></pre> <p>It occurred to me to use a conditional breakpoint here with the condition of:</p> <pre><code>primes.get(j)==false </code></pre> <p>But I'm not sure if I'm able to get meaningful info this way. These are the screens I'm getting:</p> <p><a href="http://img525.imageshack.us/img525/6238/breakpoints.jpg" rel="nofollow noreferrer">alt text http://img525.imageshack.us/img525/6238/breakpoints.jpg</a></p> <p><a href="http://img98.imageshack.us/img98/5262/watchesz.jpg" rel="nofollow noreferrer">alt text http://img98.imageshack.us/img98/5262/watchesz.jpg</a></p> <p>I don't know how to get useful info out of this.</p> <p>My questions are:</p> <p><strong>a)I want to watch the primes BitSet as its going through this loop.</strong> </p> <p><strong>How do I do that?</strong></p> <p><strong>b)</strong> <strong>What's exactly wrong with this code?</strong> <strong>How did you spot it using the debugger?</strong> </p> <p><strong>Please, mention the step-by-step process.</strong></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.
 

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