0

When I try to compile it, Eclipse says that i and j has not been initialized. What am I doing wrong? The question is about creating a circle.

public class Question2 
{

    public static void main(String[] args) {

        int x = 14;
        int y = 8;
        int radius = 5;


        DrawMeACircle(x,y,radius);

    }

    public static void DrawMeACircle(int x, int y, int radius)
    {

        int i, j;
        int gridsize = 99;
        int loop1;
        loop1 = ((x-i)*(x-i))+((y-j)*(y-j));
                // The problem is here (in the line above), it say i and j has not been initialized.

                int loop2 = loop1-radius*radius; 
                int c = radius-1; 

        for (i=0; i<gridsize; i++)
        {
            //System.out.print("#");
            for (j=0; j<gridsize; j++)
            {
                if(loop2 >=0 && loop2<= c )
                    {System.out.print("#");}
                else
                    {System.out.print(" ");}

            }
            System.out.print("\n"); 

        }
    }
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331

6 Answers6

3

Local variables must be initialized with a value:

int i = 0;
int j = 0;

Only class int variables will be set to 0 if not explicitly initialized.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

Just initialize your i and j

int i =0, j=0;

as local variables must be initialized.

As jcoder answered in this related question:- Uninitialized variables and members in Java

Instance variables of object type default to being initialized to null. Local variables of object type are not initialized by default and it's a compile time error to access an undefined variable.

Also the javadoc says:-

A local variable (§14.4, §14.14) must be explicitly given a value before it is used, by either initialization (§14.4) or assignment (§15.26), in a way that can be verified using the rules for definite assignment

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

You have to initialize i and j with a value, before you can do this loop1 = ((x-i)*(x-i))+((y-j)*(y-j)); For example int i = 0; int j = 0; Or whatever value to need it to be for your equation

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

The scope of i and j is limited to the public static void main(String[] args) method.
declaring it in the class body.
ie.
public class Question2
{
static int i, j;
public static void main(String[] args)
{
i = j = 0;
}
......... your rest of the code
}

johntheripp3r
  • 989
  • 6
  • 15
0

Declaring a variable means reserving memory space to store a value, that you already did, but, when you are going to use a variable and your IDE (in this particular case eclipse) can't find a line where you asigned the variable an initial value (or where you initialized said variable) so, you should set that first value before using the variable in any kind of operations.

int x;

system.out.println(x+1); // this line would cause the exception you got because x has not              
                         // an initial value

x=0;
system.out.println(x+1); // now x has an "initial" value and there should be no exception              
Mohsen Safari
  • 6,669
  • 5
  • 42
  • 58
Niten
  • 1
0

You're using the int variables i and j when you haven't yet initialized them. At some point in your code, you're going to have to initialize them before you use them.

int i, j;
String s = "Hello"
if (s.equals("Hello")){
    i = 0;
    j = 0;
    // Horray! We can now use i and j!
}
dtgee
  • 1,272
  • 2
  • 15
  • 30