Andy Andy - 3 months ago 6
Java Question

Compiler treating a int as a class

I am using Netbeans IDE and I am facing one small error and this ruins everything. As the title suggests, the compiler here is assuming that variable 'i' is a class instead of an integer, even though I already declared it as an integer

public class PlayerOceanGrid extends javax.swing.JFrame {

boolean play = false;
boolean deploy = false;
boolean vertical = true;
boolean horizontal = false;
boolean deploycarrier, deploycruiser, deploysubmarine, deploydestroyer, deploybattleship = false;
boolean place = false;
**int i = 0;**
int [] [] coordinate = new int [10] [10];
int randomr = (int) (Math.random() * 10);
int randomc = (int) (Math.random() * 10);
String [] shiptype = {"submarine", "cruiser", "carrier", "destroyer", "battleship"};
/* int randomtype = (int) (Math.random() * 5);
String finaltype = shiptype [randomtype];
*/
char [] facing = {'H', 'V'};
/* int randomfacing = (int) (Math.random() * 2);
char finalfacing = facing [randomfacing];
*/
int [][] coordinates = new int [10][10];

for (i = 0; i < 100; i++)
{

}


The compiler says

illegal start of type

cannot find symbol
symbol: class i
location: PlayerOceanGrid

identifier expected

And it's suggesting me to create a class named i, and that's not something I want. (I want 'i' to be used as an integer in the for loop!)

Answer

The real problem is your for loop; it isn't in a method, constructor or initializer block. So that's an illegal expression.

void myMethod() {
    for (i = 0; i < 100; i++)
    {

    }
}

But, it isn't clear what your loop is for beyond that.