• Notice regarding image hosts and attachments. See here.

null

Soldato
Joined
26 Aug 2005
Posts
6,922
Location
UK
Hi guys,

Just abit stuck on something. Basically I've got an array of IDs. I've got a if statement that checks that student doesn’t have a mark already.

Basically I have -


marks[num] != null;

Obviously you can't apply null to a int type so I was wondering how'd you do it?
 
triggerthat said:
Hi guys,

Just abit stuck on something. Basically I've got an array of IDs. I've got a if statement that checks that student doesn’t have a mark already.

Basically I have -


marks[num] != null;

Obviously you can't apply null to a int type so I was wondering how'd you do it?

Would it not be:

marks[num] != 0;
 
triggerthat said:
Hey,

It's Java. You can't applied null to int.

And as for != 0, it wont work for the first entry considering my num index will start at 0.

You mean an empty mark is indicated by the integer "0" ?
 
int values MUST always have a value in Java. Just initialise your array so they are all set to something you will never use (eg: -1) and then do if(mark!=-1).

Alternatively use an Integer[] rather than an int[]. Integer is basically an object which just wraps an int (at least, it was last time I looked). Since Integers are objects they can be null.
 
Hi,

Unfortunately the specification states that they have to of type int, and we have to define the index as 0.

Basically I just have to check the array of marks doesn’t have a mark already.
 
Why not define a new type, NulableInt, that contains an int and a Null flag?

NullableInt i ;

....code that gets a puils mark snipped....

if ( i.IsNull() )
{
...do something...
}
else
{
int pupils_mark - i.value() // i.value returns the stored int
}
 
triggerthat said:
Hey,

It's Java. You can't applied null to int.

And as for != 0, it wont work for the first entry considering my num index will start at 0.


just init your array to -1 values and check against that.
 
Back
Top Bottom