Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compare a character to check if it is null?

Tags:

java

I tried the below, but Eclipse throws an error for this.

while((s.charAt(j)== null)

What's the correct way of checking whether a character is null?

like image 680
user2062360 Avatar asked Feb 13 '13 01:02

user2062360


1 Answers

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.)

like image 61
Reimeus Avatar answered Oct 24 '22 18:10

Reimeus