Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Adding space characters into a byte array

807598Oct 24 2006 — edited Oct 24 2006
Hi Friends,
I want to initialise a byte array of 20 bytes and then fill a string into it,I want the rest of the array filled with spaces,so i tried this but its not working:
byte[] userarray = new byte[20];
for(int i=0;i<userarray.length;i++){
	userarray[i] = '\32';
}
String user = "any user";
try{
    userarray = user.getBytes("US-ASCII");
}
}catch(Exception e){e.printStackTrace();}


//Print out the byte array contents
for(int i=0;i<userName.length;i++){
	System.out.println("userarray["+i+"]"+userarray);
}


OUTPUT:
userarray[0] 97
userarray[1] 110
userarray[2] 121
userarray[3] 32
userarray[4] 117
userarray[5] 115
userarray[6] 101
userarray[7] 114

Why not???
userarray[0] 97
userarray[1] 110
userarray[2] 121
userarray[3] 32
userarray[4] 117
userarray[5] 115
userarray[6] 101
userarray[7] 114
userarray[8] 32
userarray[9] 32
..
..
..
userarray[19] 32

any help would be appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 21 2006
Added on Oct 24 2006
4 comments
893 views