char[]

J

josh

Hi, if I do:

char cop[] = new char[30];

and then

System.out.print(cop[0]+" +++ ");

why is the output an empty string and not +++ ?

Thanks
 
F

Faton Berisha

josh said:
Hi, if I do:

char cop[] = new char[30];

and then

System.out.print(cop[0]+" +++ ");

why is the output an empty string and not +++ ?

Thanks

Hint: What are the initial values of the elements of cop?
What about the character with Unicode code 0?
Maybe some research about "null terminated strings" can help.
 
G

Gordon Beaton

josh said:
Hi, if I do:

char cop[] = new char[30];

and then

System.out.print(cop[0]+" +++ ");

why is the output an empty string and not +++ ?

Thanks

Hint: What are the initial values of the elements of cop?
What about the character with Unicode code 0?
Maybe some research about "null terminated strings" can help.

Null terminated strings are not relevant here. This is Java, not C.

I get the result that Josh expected, not the one he observed.

/gordon

--
 
J

josh

josh said:
Hi, if I do:
char cop[] = new char[30];
System.out.print(cop[0]+" +++ ");
why is the output an empty string and not +++ ?

Hint: What are the initial values of the elements of cop?
What about the character with Unicode code 0?
Maybe some research about "null terminated strings" can help.

if cop is an array of char when I create it with 'new' it
should have a reference to an object of array of char with 30
cells with empty chars. But I don't understand that output!
 
M

Matt Humphrey

| > josh wrote:
| > > Hi, if I do:
| >
| > > char cop[] = new char[30];
| >
| > > and then
| >
| > > System.out.print(cop[0]+" +++ ");
| >
| > > why is the output an empty string and not +++ ?
| >
| > > Thanks
| >
| > Hint: What are the initial values of the elements of cop?
| > What about the character with Unicode code 0?
| > Maybe some research about "null terminated strings" can help.
|
| if cop is an array of char when I create it with 'new' it
| should have a reference to an object of array of char with 30
| cells with empty chars. But I don't understand that output!

Depending on how your system handles I/O, you may also need to write out a
line terminator (cr / lf) or otherwise flush the output buffer. See what
println gives instead of print. Also, are you sure you recompiled? -- this
exercise works for me.

Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
L

Lothar Kimmeringer

josh said:
if cop is an array of char when I create it with 'new' it
should have a reference to an object of array of char with 30
cells with empty chars. But I don't understand that output!

There is no such thing like an empty char. The single elements
of the char-array have the value 0 and the question is what
happens if you give that out to stdout.


Regards, Lothar
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!
 
J

josh

| > > Hi, if I do:
| >
| > > char cop[] = new char[30];
| >
| > > and then
| >
| > > System.out.print(cop[0]+" +++ ");
| >
| > > why is the output an empty string and not +++ ?
| >
| > > Thanks
| >
| > Hint: What are the initial values of the elements of cop?
| > What about the character with Unicode code 0?
| > Maybe some research about "null terminated strings" can help.
|
| if cop is an array of char when I create it with 'new' it
| should have a reference to an object of array of char with 30
| cells with empty chars. But I don't understand that output!

Depending on how your system handles I/O, you may also need to write out a
line terminator (cr / lf) or otherwise flush the output buffer. See what
println gives instead of print. Also, are you sure you recompiled? -- this
exercise works for me.

Matt Humphrey (e-mail address removed)://www.iviz.com/

also if I write:
System.out.println(x[0] + " ----- ");

in the stdout is only print the '\n' char but nothing of concatenating
x[0] with " ----- " !!!
when java meet a + sign it should create a StringBuffer tmp object
like:
new StringBuffer(x[0]).concat(" ----- " ).toString()
and than print it. But it print nothing! why?
 
C

Chris Dollin

josh said:
also if I write:
System.out.println(x[0] + " ----- ");

in the stdout is only print the '\n' char but nothing of concatenating
x[0] with " ----- " !!!
when java meet a + sign it should create a StringBuffer tmp object
like:
new StringBuffer(x[0]).concat(" ----- " ).toString()
and than print it. But it print nothing! why?

It prints something, but whatever you're using to display the
output is mishandling the nul character.

public static void main( String [] args )
{
char cop[] = new char[30];
System.out.println( ">>>" + cop[0] +" +++ " );
}

If I run this in a console, I get:

If I run it in Eclipse, I get:

If I run it in a console and pipe the output into a file, and
then look at it with vi[m], I see:

where the ^@ is a representation of the nul character.

I shall silently pass over the issue of why you were deliberately
generating output with nul characters in and then frothing and
foaming about the result rather than doing three extra minutes
of investigation on it.
 
C

cyprian

Hi, if I do:

char cop[] = new char[30];

and then

System.out.print(cop[0]+" +++ ");

why is the output an empty string and not +++ ?

Thanks

according to the java language specification, an array of char is not
a string. i checked the api and i think you should try this and see if
it works:
String[] strCop = String.copyValueOf(cop);
then
System.out.print(strCop[0]+" +++ ");
am sure it will now.
 
L

Lew

cyprian said:
Hi, if I do:

char cop[] = new char[30];

and then

System.out.print(cop[0]+" +++ ");

why is the output an empty string and not +++ ?

Thanks

according to the java language specification, an array of char is not
a string. i checked the api and i think you should try this and see if
it works:
String[] strCop = String.copyValueOf(cop);

Invalid syntax. copyValueOf() does not return an array.
then
System.out.print(strCop[0]+" +++ ");
am sure it will now.

No, it won't, whatever verb you left out. It will result in compiler error.

However, the result of "arbitraryTypeOfObject + someString" is a String, which
is what applies here, so the whole "strCop" dodge is irrelevant.

Even if you did declare "strCop" as a String and used the correct method and
syntax to get its first character, it would be literally the same type of
expression:
char plus String equals String.
 
F

Faton Berisha

josh said:
if cop is an array of char when I create it with 'new' it
should have a reference to an object of array of char with 30
cells with empty chars. But I don't understand that output!

It would create an array with null characters, which are different form
empty char.

Faton Berisha
 
F

Faton Berisha

Gordon said:
Null terminated strings are not relevant here. This is Java, not C.

I get the result that Josh expected, not the one he observed.

/gordon

What is relevant here is the fact that mull characters are not expected
to be handled by a printing device.

Obviously, in OP's case it takes such a character for a string terminator.

Faton Berisha
 
C

cyprian

cyprian said:
Hi, if I do:
char cop[] = new char[30];
and then
System.out.print(cop[0]+" +++ ");
why is the output an empty string and not +++ ?
Thanks
according to the java language specification, an array of char is not
a string. i checked the api and i think you should try this and see if
it works:
String[] strCop = String.copyValueOf(cop);

Invalid syntax. copyValueOf() does not return an array.
then
System.out.print(strCop[0]+" +++ ");
am sure it will now.

No, it won't, whatever verb you left out. It will result in compiler error.

However, the result of "arbitraryTypeOfObject + someString" is a String, which
is what applies here, so the whole "strCop" dodge is irrelevant.

Even if you did declare "strCop" as a String and used the correct method and
syntax to get its first character, it would be literally the same type of
expression:
char plus String equals String.

i think i was really in a hurry. String[] references String objects in
the array varaible, strCop. so i guessed he should should read in each
reference singly, i asked him to read it as a whole. read singly like
this:

String[] strCop = new String[copy.length];
for (int i=0; i<cop.length; i++){
//do the valueOfCopy here, component by component.
strCopy = String.valueOfCopy(cop);
/....everything should be okay from here
}

my mistake. i just know it'll work. an IDE not within reach though.
it will this time
 
C

cyprian

cyprian said:
Hi, if I do:
char cop[] = new char[30];
and then
System.out.print(cop[0]+" +++ ");
why is the output an empty string and not +++ ?
Thanks
according to the java language specification, an array of char is not
a string. i checked the api and i think you should try this and see if
it works:
String[] strCop = String.copyValueOf(cop);
Invalid syntax. copyValueOf() does not return an array.
then
System.out.print(strCop[0]+" +++ ");
am sure it will now.
No, it won't, whatever verb you left out. It will result in compiler error.
However, the result of "arbitraryTypeOfObject + someString" is a String, which
is what applies here, so the whole "strCop" dodge is irrelevant.
Even if you did declare "strCop" as a String and used the correct method and
syntax to get its first character, it would be literally the same type of
expression:
char plus String equals String.

i think i was really in a hurry. String[] references String objects in
the array varaible, strCop. so i guessed he should should read in each
reference singly, i asked him to read it as a whole. read singly like
this:

String[] strCop = new String[copy.length];
for (int i=0; i<cop.length; i++){
//do the valueOfCopy here, component by component.
strCopy = String.valueOfCopy(cop);
/....everything should be okay from here

}

my mistake. i just know it'll work. an IDE not within reach though.
it will this time


got to hide my head in shame. i tried my code above at home...
well, the asker didn't post wrong code then all he just didn't
understsand was the result syso returned. default for char values is
\u0000 which has a code point of -1 and doesn't map to string the same
way return key and tab key do not map to string.
hmphhh...

char[] cop = new char[30];
Character y = new Character('k');
System.out.println(y.getNumericValue('k'));
System.out.println(y.getNumericValue(cop[0])); //unicode maps
from code point zero
System.out.println(y.getNumericValue('\u0000'));
if (y.isDefined(cop[0]))
System.out.println("yes"); //yes

}
//\u0000 is the first unicode character, rather a symbol, like
\u0009 is the
tab character or your '\t'.
 
L

Lew

cyprian said:
got to hide my head in shame. i tried my code above at home...
well, the asker didn't post wrong code then all he just didn't
understsand was the result syso returned. default for char values is
\u0000 which has a code point of -1 and doesn't map to string the same
way return key and tab key do not map to string.
hmphhh...

Chars don't "map to" Strings, Strings comprise chars.

Unicode '\u0000' is a perfectly valid char, even in a String. Not all display
devices handle it the same way.

Many posters have made that point to the OP.
 

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
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top