-
Variable uninitialized
It says the variable gives might be uninitialized at the end where the statement start=gives+2;, anyone know why this is?
PHP Code:
int gives;
int it = 0;
for(int i=0; i<l; i++)
{
if(names[i].equals(v.get(start+l+1)))
{
String num = v.get(start+l+2);
String[] data = num.split(" ");
given[i] = Integer.parseInt(data[0]);
gives = Integer.parseInt(data[1]);
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++)
{
if(names[m].equals(v.get(start+k+l+3)))
{
received[m] += given[i]/gives;
System.out.print(received[m] + " ");
}
}
}
}
System.out.print(received[i] + " ");
start = gives+2;
}
-
If this line:
Code:
if(names[i].equals(v.get(start+l+1)))
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.
I.e.
-
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[5];
and then like
if(othervar == othervar)
{
set some values of blah
}
and then can i see if all the values of blah are set?
thanks
-
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks