How do I compare a character to check if it is null?
Default value to char primitives is 0 , as its ascii value. you can check char if it is null. for eg:
char ch[] = new char[20]; //here the whole array will be initialized with '\u0000' i.e. 0
if((int)ch[0]==0){
System.out.println("char is null");
}
You can use the null character ('\0'
):
while((s.charAt(j)=='\0')
Correct way of checking char
is actually described here.
It states:
Change it to:
if(position[i][j] == 0)
Eachchar
can be compared with anint
. The default value is'\u0000'
i.e. 0 for achar
array element. And that's exactly what you meant by empty cell, I assume.
Check that the String
s
is not null
before doing any character checks. The characters returned by String#charAt
are primitive char
types and will never be null
:
if (s != null) {
...
If you're trying to process characters from String
one at a time, you can use:
for (char c: s.toCharArray()) {
// do stuff with char c
}
(Unlike C
, NULL
terminator checking is not done in Java.)