It says the variable gives might be uninitialized at the end where the statement start=gives+2;, anyone know why this is?
int it = 0;
for(int i=0; i<l; i++)
String num = v.get(start+l+2);
String data = num.split(" ");
given[i] = Integer.parseInt(data);
gives = Integer.parseInt(data);
System.out.print(gives + " ");
if((rem = given[i]%gives) != 0)
received[i] = rem;
for(int k=0; k<gives; k++)
System.out.print(k + " ");
for(int m=0; m<l; m++)
received[m] += given[i]/gives;
System.out.print(received[m] + " ");
System.out.print(received[i] + " ");
start = gives+2;
If this line:
evaluates to false, then your variable gives is never assigned a value before it is referenced on the last line. When you declare the variable, you should initialize it to some value.
Ok i didn't think id make a new thread but is there a way to see if every value in an array is set? Like if i initialize an array like
int blah = new int;
and then like
if(othervar == othervar)
set some values of blah
and then can i see if all the values of blah are set?
When you create the array using the new keyword, each array element is automatically initialised to the default value for the primitive. (Primitive types include bytes, chars, ints, longs, floats, etc, etc.) The defaut value for the integer type is 0, so all elements will automatically be initialised to 0. Thus, all the elements are already 'set'.
To summarise and avoid any further confusion...
If you declare a primitive variable (e.g. byte, char, int, float, etc.) as a class/instance variable (i.e. at the class level, not within a method), then the variable is automatically assigned its default value, assuming you don't assign a value yourself. For numeric primitives, this is always 0. For booleans, the default is false.
Array elements behave the same way, wherever/whenever (sorry for the Shakira link there) they are created using the new keyword.
Objects declared as class/instance members are automatically initialized to null.
If you declare a primitive variable within a method, the variable is local to whatever block the variable is declared in, and the variable is NOT initialised. This is why you have to initialize local variables before you use them. Similarly, objects declared with local scope are not initialized. To avoid compiler errors, it's often a good idea to initialize them to null at declaration time.