I have a char array which need to check each and every character untill there is no more character to check,
so in C usually we write as:

for(i=0;my_num_Array[i]!='\0';i++)
{
 // some code here
 }

But when i tried the same for java it isn't working!
Any idea what should I do?

In Java char is a 16 bit unsigned numeric value, so zero is perfectly valid anywhere, but the Java equivalent of your loop wold be something like
for (int i = 0; my-char-array[i] != 0; i++) {...
of course that will give an array index out of bounds if the array contains no zeros.

What areyou really trying to achieve?

I don't think an array of char can have an element that is null. It can have an element with a value of 0.

isn't working

Can you post the code that is not working?

Is you problem using a for loop? It will stop looping when the condition is met. It won't continue.

Edited 4 Years Ago by NormR1

^ yes, that's right. char is a primitive type, and only reference types can be null. I think the OP is referring to the C convention of representing strings as arrays of characters with a zero at the end.

So you guys are saying in Java there is no way to check if my_char_array[i]!= NULL

There's no such entity as NULL in Java. There is null, but that is not a valid value for a char variable. A char can have a value of zero, but that has no particular significance. It looks like you're trying to apply a C idiom in Java in a way that doesn't apply. Tell us what is it you are trying to achieve, and we'll tell you how to do it in Java.

Edited 4 Years Ago by JamesCherrill

I have a char Array which has some charcters (input from user). So I don't know how many characters are there in this array. Maybe if I could find the length of the array, Right?

How do you get them from the user? Why not just accept them as a String?

(In Java user input, whether from a GUI text field, a dialog box, or even a command-line console, is read as a String object. A String contains a sequence of chars, knows its own length, and comes with a huge choice of useful methods for doing text--related stuff)

Edited 4 Years Ago by JamesCherrill

Heys guys.....can you tell me the internal working of string.length() method.....i mean to say that i want to create my user defined method?????

What exactly do you wan to know? String's length() nethod returns the length of the String, or more precisely the number of Unicode code units in the string (some code units require two characters to represent). That's all you need to know. Refer to the API documentation for all the public info on Strings. The implementation is highly optimised because Strings are such a fundamental Java data type, eg under some circumstances different Strings may share the same underlying character arrays to minimise storage. All you can rely on is the public API - if it's not documented here then you can't rely on it.
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

It still looks like you're trying to apply C thinking to Java in a way that doesn't apply. Tell us what is it you are trying to achieve, and we'll tell you how to do it in Java.

Edited 4 Years Ago by JamesCherrill

Govind: your answer is not correct ('\u0020' is NOT the same as null) and a year overdue. no reason to revive a dead thread.

Edited 2 Years Ago by stultuske

assuming you have a byte array and you want to search by index for null character,

int index = new String(bytearray).indexOf((char) (byte) 0));

if index returns -1, then null character is not found.

This question has already been answered. Start a new discussion instead.