Ask coding questions

← Back to all posts
Why can't I print this line?
Travelsbyfire (1)

One of my print statements, which is an exact copy as the other, is printing in the same method, but not in the for loop, and it works. The print in the for loop breaks the code with this

OpenJDK Runtime Environment (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)
 javac -classpath .:/run_dir/junit-4.12.jar -d . Main.java
Main.java:24: error: cannot find symbol
heapify(a, n, i);
^
symbol: variable i
location: class Main
1 error
compiler exit status 1

public void sort(int a[]) {
int n = a.length;

System.out.println("n " + " vs " + ((n/2)-1));          //doesn't break the code

// create heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
      System.out.println("n " + " vs " + ((n/2)-1));      // breaks the code
   
   heapify(a, n, i);

// remove max
for (int i = n - 1; i >= 0; i--) {
  // Move current root to end
  int temp = a[0];
  a[0] = a[i];
  a[i] = temp;

  // call max heapify on the reduced heap
  heapify(a, i, 0);
}

}

Commentshotnewtop
Travelsbyfire (1)

It's because you forgot to put brackets around the for loop