Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

byte value in a code

letsgoforitletsgoforit Posts: 53Member
here is a code which prompts user to write his name and then greets him by usuing hello...

i want to know some facts..

[code]import java.io.*;

class PersonalHello {

public static void main (String args[])
{

byte name[] = new byte[100];
int nr_read = 0;

System.out.println("What is your name?");
try {
nr_read = System.in.read(name);

System.out.println ( nr_read ); // line 1

System.out.print("Hello ");
System.out.write(name,0,nr_read);
}
catch (IOException e) {
System.out.print("I'm Sorry. I didn't catch your name.");
}

}

}
[/code]

this code is running...i am studying from a site.

my question
-----------
1. i usued line 1 to see the int value returned by[b] nr_read [/b]

my input was [b]abcd[/b]

output : 6, hello abcd

how this 6 is coming????????????? how can i calculate ???

Comments

  • fzammettifzammetti Posts: 68Member
    System.in.read returns the total number of bytes read into the buffer. You typed four four characters, so that's four bytes obviously. When you hit return, you got a carriage return and a linefeed as well. To prove this to yourself, add this:

    for (int i = 0; i < 6; i++) {
    System.out.println(new Byte(name[i]));
    }

    After the line:

    System.out.write(name,0,nr_read);

    Note that the last two numbers shown is 13 and 10, which is carriage return and linefeed in ASCII.

    : here is a code which prompts user to write his name and then greets him by usuing hello...
    :
    : i want to know some facts..
    :
    : [code]import java.io.*;
    :
    : class PersonalHello {
    :
    : public static void main (String args[])
    : {
    :
    : byte name[] = new byte[100];
    : int nr_read = 0;
    :
    : System.out.println("What is your name?");
    : try {
    : nr_read = System.in.read(name);
    :
    : System.out.println ( nr_read ); // line 1
    :
    : System.out.print("Hello ");
    : System.out.write(name,0,nr_read);
    : }
    : catch (IOException e) {
    : System.out.print("I'm Sorry. I didn't catch your name.");
    : }
    :
    : }
    :
    : }
    : [/code]
    :
    : this code is running...i am studying from a site.
    :
    : my question
    : -----------
    : 1. i usued line 1 to see the int value returned by[b] nr_read [/b]
    :
    : my input was [b]abcd[/b]
    :
    : output : 6, hello abcd
    :
    : how this 6 is coming????????????? how can i calculate ???
    :

  • letsgoforitletsgoforit Posts: 53Member
    [code]for (int i = 0; i < 6; i++) {
    System.out.println(new Byte(name[i]));[/code]

    yes it is printing ascii values of letters...

    i have some question on this....

    (1)you are usuing "new Byte(name[i])"--
    ---that means you are basically creating a Byte object..right? does this println() function takes Byte object as an argument??? till now i knew it takes String,int etc primitive data types as argument.does it allow any object??? if it has [b] more fields[/b] then how can it print???

    (2)System.out.println([red](char)[/red](name[i]));--> why this is not working???i am getting compilation error..
    i tried also
    System.out.println([red](String)[/red](name[i]));...error

    thanks



    : System.in.read returns the total number of bytes read into the buffer. You typed four four characters, so that's four bytes obviously. When you hit return, you got a carriage return and a linefeed as well. To prove this to yourself, add this:
    :
    : for (int i = 0; i < 6; i++) {
    : System.out.println(new Byte(name[i]));
    : }
    :
    : After the line:
    :
    : System.out.write(name,0,nr_read);
    :
    : Note that the last two numbers shown is 13 and 10, which is carriage return and linefeed in ASCII.
    :
    : : here is a code which prompts user to write his name and then greets him by usuing hello...
    : :
    : : i want to know some facts..
    : :
    : : [code]import java.io.*;
    : :
    : : class PersonalHello {
    : :
    : : public static void main (String args[])
    : : {
    : :
    : : byte name[] = new byte[100];
    : : int nr_read = 0;
    : :
    : : System.out.println("What is your name?");
    : : try {
    : : nr_read = System.in.read(name);
    : :
    : : System.out.println ( nr_read ); // line 1
    : :
    : : System.out.print("Hello ");
    : : System.out.write(name,0,nr_read);
    : : }
    : : catch (IOException e) {
    : : System.out.print("I'm Sorry. I didn't catch your name.");
    : : }
    : :
    : : }
    : :
    : : }
    : : [/code]
    : :
    : : this code is running...i am studying from a site.
    : :
    : : my question
    : : -----------
    : : 1. i usued line 1 to see the int value returned by[b] nr_read [/b]
    : :
    : : my input was [b]abcd[/b]
    : :
    : : output : 6, hello abcd
    : :
    : : how this 6 is coming????????????? how can i calculate ???
    : :
    :
    :

  • fzammettifzammetti Posts: 68Member
    (1) Yes, it's creating a Byte object, which is a wrapper class, around the primitive byte from the array. System.out.println is a PrintStream, which accepts a rather large number of parameters (just look through the javadocs). The one that is relevant here though is Object. Recall that the Object class has a method toString(), which means that EVERY object in Java inherently has a toString method (since everything inherits from Object, one way or another). Now, it may or may not do what you want (i.e., sometimes it's some rather cryptic information, or maybe nothing at all even), but it's always there. The way it works is that there is an implicit call to toString() when you pass any object to System.out.println. Therefore, you can toss ANY object to System.out.prinltn and you'll get something. Whether it's what you want is of course another story. Note that in your own classes you can always override toString() to output something relevant. I'm not sure what you meant when you asked about it having more fields... I'll take a guess though... If you meant "what if the object has a number of fields, how does it know which to display?", the answer should now be obvious: it's a function of what the toString() method in the class does. It may print all the members as a comma-separated list (try passing a HashMap or ArrayList to System.out.println to see), or it may output a picture of Britney Sears in ASCII art. Whatever the class programmer did in toString is what you'll get, but your guaranteed to always get SOMETHING.

    (2) I THINK the reason your getting a compilation error is because some casts are not possible in Java. Are you getting an incompatible type error, or something worded like that? That's an indication that the cast your trying is simply not valid (might just be a case of parenthesis in the wrong place... maybe the compiler thinks your trying to cast the entire array to a String, rather than a single byte). Actually, in the case of the (String) cast your trying, that's obviously bad... you can't cast a primitive byte to a String object. That doesn't logically make sense. The other one I'd have to think about a bit more, I might have expected that to work too.

    : [code]for (int i = 0; i < 6; i++) {
    : System.out.println(new Byte(name[i]));[/code]
    :
    : yes it is printing ascii values of letters...
    :
    : i have some question on this....
    :
    : (1)you are usuing "new Byte(name[i])"--
    : ---that means you are basically creating a Byte object..right? does this println() function takes Byte object as an argument??? till now i knew it takes String,int etc primitive data types as argument.does it allow any object??? if it has [b] more fields[/b] then how can it print???
    :
    : (2)System.out.println([red](char)[/red](name[i]));--> why this is not working???i am getting compilation error..
    : i tried also
    : System.out.println([red](String)[/red](name[i]));...error
    :
    : thanks
    :
    :
    :
    : : System.in.read returns the total number of bytes read into the buffer. You typed four four characters, so that's four bytes obviously. When you hit return, you got a carriage return and a linefeed as well. To prove this to yourself, add this:
    : :
    : : for (int i = 0; i < 6; i++) {
    : : System.out.println(new Byte(name[i]));
    : : }
    : :
    : : After the line:
    : :
    : : System.out.write(name,0,nr_read);
    : :
    : : Note that the last two numbers shown is 13 and 10, which is carriage return and linefeed in ASCII.
    : :
    : : : here is a code which prompts user to write his name and then greets him by usuing hello...
    : : :
    : : : i want to know some facts..
    : : :
    : : : [code]import java.io.*;
    : : :
    : : : class PersonalHello {
    : : :
    : : : public static void main (String args[])
    : : : {
    : : :
    : : : byte name[] = new byte[100];
    : : : int nr_read = 0;
    : : :
    : : : System.out.println("What is your name?");
    : : : try {
    : : : nr_read = System.in.read(name);
    : : :
    : : : System.out.println ( nr_read ); // line 1
    : : :
    : : : System.out.print("Hello ");
    : : : System.out.write(name,0,nr_read);
    : : : }
    : : : catch (IOException e) {
    : : : System.out.print("I'm Sorry. I didn't catch your name.");
    : : : }
    : : :
    : : : }
    : : :
    : : : }
    : : : [/code]
    : : :
    : : : this code is running...i am studying from a site.
    : : :
    : : : my question
    : : : -----------
    : : : 1. i usued line 1 to see the int value returned by[b] nr_read [/b]
    : : :
    : : : my input was [b]abcd[/b]
    : : :
    : : : output : 6, hello abcd
    : : :
    : : : how this 6 is coming????????????? how can i calculate ???
    : : :
    : :
    : :
    :
    :

  • letsgoforitletsgoforit Posts: 53Member
    hi, thank you for the reply...
    [code]The other one I'd have to think about a bit more, I might have expected that to work too.
    [/code]....yes casting into (char) is working...i did a mistake.

    i tried also [b] String(name)[/b]....thinking that

    (a)first it will convert that entire array (starting from &name[0])to a String object...and as you pointed earlier println() supports any object so it will print that String object
    but got error like below...
    [b]PersonalHello.java:15: inconvertible types
    found : byte[]
    required: java.lang.String
    System.out.println((String)name);
    ^
    1 error [/b]

    ur comment " you can't cast a primitive byte to a String object "...is important...but i thought i am going to print a bunch of bytes starting from &name[0] so whats wrong if i caste it to a String object(as println() function takes any object)

    ok...i need wrapper class



    : (1) Yes, it's creating a Byte object, which is a wrapper class, around the primitive byte from the array. System.out.println is a PrintStream, which accepts a rather large number of parameters (just look through the javadocs). The one that is relevant here though is Object. Recall that the Object class has a method toString(), which means that EVERY object in Java inherently has a toString method (since everything inherits from Object, one way or another). Now, it may or may not do what you want (i.e., sometimes it's some rather cryptic information, or maybe nothing at all even), but it's always there. The way it works is that there is an implicit call to toString() when you pass any object to System.out.println. Therefore, you can toss ANY object to System.out.prinltn and you'll get something. Whether it's what you want is of course another story. Note that in your own classes you can always override toString() to output something relevant. I'm not sure what you meant when you asked about it having more fields... I'll take a guess though... If you meant "what if the object has a number of fields, how does it know which to display?", the answer should now be obvious: it's a function of what the toString() method in the class does. It may print all the members as a comma-separated list (try passing a HashMap or ArrayList to System.out.println to see), or it may output a picture of Britney Sears in ASCII art. Whatever the class programmer did in toString is what you'll get, but your guaranteed to always get SOMETHING.
    :
    : (2) I THINK the reason your getting a compilation error is because some casts are not possible in Java. Are you getting an incompatible type error, or something worded like that? That's an indication that the cast your trying is simply not valid (might just be a case of parenthesis in the wrong place... maybe the compiler thinks your trying to cast the entire array to a String, rather than a single byte). Actually, in the case of the (String) cast your trying, that's obviously bad... you can't cast a primitive byte to a String object. That doesn't logically make sense. The other one I'd have to think about a bit more, I might have expected that to work too.
    :
    : : [code]for (int i = 0; i < 6; i++) {
    : : System.out.println(new Byte(name[i]));[/code]
    : :
    : : yes it is printing ascii values of letters...
    : :
    : : i have some question on this....
    : :
    : : (1)you are usuing "new Byte(name[i])"--
    : : ---that means you are basically creating a Byte object..right? does this println() function takes Byte object as an argument??? till now i knew it takes String,int etc primitive data types as argument.does it allow any object??? if it has [b] more fields[/b] then how can it print???
    : :
    : : (2)System.out.println([red](char)[/red](name[i]));--> why this is not working???i am getting compilation error..
    : : i tried also
    : : System.out.println([red](String)[/red](name[i]));...error
    : :
    : : thanks
    : :
    : :
    : :
    : : : System.in.read returns the total number of bytes read into the buffer. You typed four four characters, so that's four bytes obviously. When you hit return, you got a carriage return and a linefeed as well. To prove this to yourself, add this:
    : : :
    : : : for (int i = 0; i < 6; i++) {
    : : : System.out.println(new Byte(name[i]));
    : : : }
    : : :
    : : : After the line:
    : : :
    : : : System.out.write(name,0,nr_read);
    : : :
    : : : Note that the last two numbers shown is 13 and 10, which is carriage return and linefeed in ASCII.
    : : :
    : : : : here is a code which prompts user to write his name and then greets him by usuing hello...
    : : : :
    : : : : i want to know some facts..
    : : : :
    : : : : [code]import java.io.*;
    : : : :
    : : : : class PersonalHello {
    : : : :
    : : : : public static void main (String args[])
    : : : : {
    : : : :
    : : : : byte name[] = new byte[100];
    : : : : int nr_read = 0;
    : : : :
    : : : : System.out.println("What is your name?");
    : : : : try {
    : : : : nr_read = System.in.read(name);
    : : : :
    : : : : System.out.println ( nr_read ); // line 1
    : : : :
    : : : : System.out.print("Hello ");
    : : : : System.out.write(name,0,nr_read);
    : : : : }
    : : : : catch (IOException e) {
    : : : : System.out.print("I'm Sorry. I didn't catch your name.");
    : : : : }
    : : : :
    : : : : }
    : : : :
    : : : : }
    : : : : [/code]
    : : : :
    : : : : this code is running...i am studying from a site.
    : : : :
    : : : : my question
    : : : : -----------
    : : : : 1. i usued line 1 to see the int value returned by[b] nr_read [/b]
    : : : :
    : : : : my input was [b]abcd[/b]
    : : : :
    : : : : output : 6, hello abcd
    : : : :
    : : : : how this 6 is coming????????????? how can i calculate ???
    : : : :
    : : :
    : : :
    : :
    : :
    :
    :

  • fzammettifzammetti Posts: 68Member
    Well, here's a good example of toString() always being there but not always doing what you want...

    You CAN in fact pass your name array to println:

    System.out.println(name);

    Because in Java, arrays (even of primitives) are objects. Therefore, it has a toString() method for println to call on. But, if you do it, what you'll actually get is an internal reference to the array object, not the contents of the array.

    But expecting the array to get converted to a string was a reasonable thought because the designers of Java could have implemented toString() to do just that. They choose not to though, so you get the object reference ID instead.

    : hi, thank you for the reply...
    : [code]The other one I'd have to think about a bit more, I might have expected that to work too.
    : [/code]....yes casting into (char) is working...i did a mistake.
    :
    : i tried also [b] String(name)[/b]....thinking that
    :
    : (a)first it will convert that entire array (starting from &name[0])to a String object...and as you pointed earlier println() supports any object so it will print that String object
    : but got error like below...
    : [b]PersonalHello.java:15: inconvertible types
    : found : byte[]
    : required: java.lang.String
    : System.out.println((String)name);
    : ^
    : 1 error [/b]
    :
    : ur comment " you can't cast a primitive byte to a String object "...is important...but i thought i am going to print a bunch of bytes starting from &name[0] so whats wrong if i caste it to a String object(as println() function takes any object)
    :
    : ok...i need wrapper class
    :
    :
    :
    : : (1) Yes, it's creating a Byte object, which is a wrapper class, around the primitive byte from the array. System.out.println is a PrintStream, which accepts a rather large number of parameters (just look through the javadocs). The one that is relevant here though is Object. Recall that the Object class has a method toString(), which means that EVERY object in Java inherently has a toString method (since everything inherits from Object, one way or another). Now, it may or may not do what you want (i.e., sometimes it's some rather cryptic information, or maybe nothing at all even), but it's always there. The way it works is that there is an implicit call to toString() when you pass any object to System.out.println. Therefore, you can toss ANY object to System.out.prinltn and you'll get something. Whether it's what you want is of course another story. Note that in your own classes you can always override toString() to output something relevant. I'm not sure what you meant when you asked about it having more fields... I'll take a guess though... If you meant "what if the object has a number of fields, how does it know which to display?", the answer should now be obvious: it's a function of what the toString() method in the class does. It may print all the members as a comma-separated list (try passing a HashMap or ArrayList to System.out.println to see), or it may output a picture of Britney Sears in ASCII art. Whatever the class programmer did in toString is what you'll get, but your guaranteed to always get SOMETHING.
    : :
    : : (2) I THINK the reason your getting a compilation error is because some casts are not possible in Java. Are you getting an incompatible type error, or something worded like that? That's an indication that the cast your trying is simply not valid (might just be a case of parenthesis in the wrong place... maybe the compiler thinks your trying to cast the entire array to a String, rather than a single byte). Actually, in the case of the (String) cast your trying, that's obviously bad... you can't cast a primitive byte to a String object. That doesn't logically make sense. The other one I'd have to think about a bit more, I might have expected that to work too.
    : :
    : : : [code]for (int i = 0; i < 6; i++) {
    : : : System.out.println(new Byte(name[i]));[/code]
    : : :
    : : : yes it is printing ascii values of letters...
    : : :
    : : : i have some question on this....
    : : :
    : : : (1)you are usuing "new Byte(name[i])"--
    : : : ---that means you are basically creating a Byte object..right? does this println() function takes Byte object as an argument??? till now i knew it takes String,int etc primitive data types as argument.does it allow any object??? if it has [b] more fields[/b] then how can it print???
    : : :
    : : : (2)System.out.println([red](char)[/red](name[i]));--> why this is not working???i am getting compilation error..
    : : : i tried also
    : : : System.out.println([red](String)[/red](name[i]));...error
    : : :
    : : : thanks
    : : :
    : : :
    : : :
    : : : : System.in.read returns the total number of bytes read into the buffer. You typed four four characters, so that's four bytes obviously. When you hit return, you got a carriage return and a linefeed as well. To prove this to yourself, add this:
    : : : :
    : : : : for (int i = 0; i < 6; i++) {
    : : : : System.out.println(new Byte(name[i]));
    : : : : }
    : : : :
    : : : : After the line:
    : : : :
    : : : : System.out.write(name,0,nr_read);
    : : : :
    : : : : Note that the last two numbers shown is 13 and 10, which is carriage return and linefeed in ASCII.
    : : : :
    : : : : : here is a code which prompts user to write his name and then greets him by usuing hello...
    : : : : :
    : : : : : i want to know some facts..
    : : : : :
    : : : : : [code]import java.io.*;
    : : : : :
    : : : : : class PersonalHello {
    : : : : :
    : : : : : public static void main (String args[])
    : : : : : {
    : : : : :
    : : : : : byte name[] = new byte[100];
    : : : : : int nr_read = 0;
    : : : : :
    : : : : : System.out.println("What is your name?");
    : : : : : try {
    : : : : : nr_read = System.in.read(name);
    : : : : :
    : : : : : System.out.println ( nr_read ); // line 1
    : : : : :
    : : : : : System.out.print("Hello ");
    : : : : : System.out.write(name,0,nr_read);
    : : : : : }
    : : : : : catch (IOException e) {
    : : : : : System.out.print("I'm Sorry. I didn't catch your name.");
    : : : : : }
    : : : : :
    : : : : : }
    : : : : :
    : : : : : }
    : : : : : [/code]
    : : : : :
    : : : : : this code is running...i am studying from a site.
    : : : : :
    : : : : : my question
    : : : : : -----------
    : : : : : 1. i usued line 1 to see the int value returned by[b] nr_read [/b]
    : : : : :
    : : : : : my input was [b]abcd[/b]
    : : : : :
    : : : : : output : 6, hello abcd
    : : : : :
    : : : : : how this 6 is coming????????????? how can i calculate ???
    : : : : :
    : : : :
    : : : :
    : : :
    : : :
    : :
    : :
    :
    :

Sign In or Register to comment.