End of String

J

Jeffrey Schwab

Javier said:
Jeffrey Schwab ha escrito:



Then you'd better use StringBuffer instead:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html

and having the code \0000 to delimit the valid content of a buffer is
not always a good idea.

Agreed. In fact, it's rarely a good idea in Java. But that doesn't
mean it can't be done, and in fact there might even be real situations
where it's useful. You might want to review the earlier parts of this
thread, especially the part you snipped.
There may be valid content beyond that.

Eh? No, by definition, it cannot. We've specifically defined the
buffer to *not* have valid content beyond the null character. That is
the meaning of null in this context. If a given source of input can
produce valid, zero-valued characters, they must be escaped for the
delimeter to be meaningful.

Here's a good example of the same technique being used as part of the
interface to ArrayList:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html#toArray(T[])

"If the list fits in the specified array with room to spare (i.e., the
array has more elements than the list), the element in the array
immediately following the end of the collection is set to null. This is
useful in determining the length of the list only if the caller knows
that the list does not contain any null elements."
 
J

Javier

Jeffrey Schwab ha escrito:
Javier said:
Jeffrey Schwab ha escrito:



Then you'd better use StringBuffer instead:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html

and having the code \0000 to delimit the valid content of a buffer is
not always a good idea.

Agreed. In fact, it's rarely a good idea in Java. But that doesn't
mean it can't be done, and in fact there might even be real situations
where it's useful. You might want to review the earlier parts of this
thread, especially the part you snipped.
There may be valid content beyond that.

Eh? No, by definition, it cannot. We've specifically defined the
buffer to *not* have valid content beyond the null character. That is
the meaning of null in this context. If a given source of input can
produce valid, zero-valued characters, they must be escaped for the
delimeter to be meaningful.

Here's a good example of the same technique being used as part of the
interface to ArrayList:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html#toArray(T[])

"If the list fits in the specified array with room to spare (i.e., the
array has more elements than the list), the element in the array
immediately following the end of the collection is set to null. This is
useful in determining the length of the list only if the caller knows
that the list does not contain any null elements."

Sorry if I was not clear. I agree with you in that in some case is
usefull. I meant that there are times in which \u0000 might be
considered a valid character, for example not for limiting the end of
the buffer, but the end of a chunk or something similar. Even sometimes
could be just ignored. Or even it might be a part of some kind of
encription, etc.
According to the text you quote, ArrayList implements null, which I
believe is not the same as \u0000 (correct me if I'm wrong).
 
J

Jeffrey Schwab

Javier said:
Jeffrey Schwab ha escrito:
Javier said:
Jeffrey Schwab ha escrito:


Remember that arrays in Java _also_ have the length attribute.
That's not the same thing. What's of interest here is not the size of
the buffer, but the amount of valid content it contains.
Then you'd better use StringBuffer instead:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuffer.html

and having the code \0000 to delimit the valid content of a buffer is
not always a good idea.
Agreed. In fact, it's rarely a good idea in Java. But that doesn't
mean it can't be done, and in fact there might even be real situations
where it's useful. You might want to review the earlier parts of this
thread, especially the part you snipped.
There may be valid content beyond that.
Eh? No, by definition, it cannot. We've specifically defined the
buffer to *not* have valid content beyond the null character. That is
the meaning of null in this context. If a given source of input can
produce valid, zero-valued characters, they must be escaped for the
delimeter to be meaningful.

Here's a good example of the same technique being used as part of the
interface to ArrayList:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html#toArray(T[])

"If the list fits in the specified array with room to spare (i.e., the
array has more elements than the list), the element in the array
immediately following the end of the collection is set to null. This is
useful in determining the length of the list only if the caller knows
that the list does not contain any null elements."

Sorry if I was not clear. I agree with you in that in some case is
usefull. I meant that there are times in which \u0000 might be
considered a valid character, for example not for limiting the end of
the buffer, but the end of a chunk or something similar. Even sometimes
could be just ignored. Or even it might be a part of some kind of
encription, etc.

Right, the value zero (0) has other uses than being a delimiter.
According to the text you quote, ArrayList implements null, which I
believe is not the same as \u0000 (correct me if I'm wrong).

You are again correct '\u0000' is the null character value, not the null
reference. ArrayList uses a null reference, because Lists hold
references, not primitive chars.
 

neo

Joined
Aug 21, 2011
Messages
1
Reaction score
0
please help me!!
check it for me. thank!


import java.lang.*;
import java.util.*;
public class bai5_2 {
public static void timkiem()
{
Scanner nhapvao=new Scanner(System.in);
System.out.println(" String :");
String str1=new String();
str1=nhapvao.nextLine();
int n=str1.length();
int i,dem,m;i=0;
dem=i;

System.out.print(str1);
i=0;
while(i<n)
{
if(str1.charAt(i)==' '){while(str1.charAt(i)==' ') i++; }
else {dem++;while(str1.charAt(i)!=' ') i++;}
}
System.out.println(dem);

}
public static void main(String args[]){
timkiem();
}



}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top