![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jul 2005
Posts: 73
Rep Power: 4
![]() |
I/O question
this program was in a tutorial:
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.print("Hello ");
System.out.write(name,0,nr_read);
}
catch (IOException e) {
System.out.print("I'm Sorry. I didn't catch your name.");
}
}
}1. what does System.out.write() do? 2. why does it write() "name,0,nr_read" instead of just name? what does nr_read do? what is the 0 there for? edit: after some experiments, i found that name is a seemingly random strng of characters. I got "[B@10b62c9". nr_read is the number of characters entered plus 2. (when I entered "Matt" nr_read was 6, when I entered "hi" nr_read was 4) Last edited by theguy0000; Dec 21st, 2005 at 11:28 AM. |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Name is a random string of characters because an array basically points to the memory address where your array starts. When you System.out.prinln(name) you will get the memory address where name is stored.
For more information on what write() does, check here: http://java.sun.com/j2se/1.5.0/docs/...intStream.html
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jul 2005
Posts: 73
Rep Power: 4
![]() |
oh...
then how do i convert the byte to a string and put it in a variable? |
|
|
|
|
|
#4 |
|
Expert Programmer
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|