Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 21st, 2005, 11:02 AM   #1
theguy0000
Programmer
 
Join Date: Jul 2005
Posts: 73
Rep Power: 4 theguy0000 is on a distinguished road
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.
theguy0000 is offline   Reply With Quote
Old Dec 21st, 2005, 1:17 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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
Mjordan2nd is offline   Reply With Quote
Old Dec 21st, 2005, 6:22 PM   #3
theguy0000
Programmer
 
Join Date: Jul 2005
Posts: 73
Rep Power: 4 theguy0000 is on a distinguished road
oh...

then how do i convert the byte to a string and put it in a variable?
theguy0000 is offline   Reply With Quote
Old Dec 21st, 2005, 7:22 PM   #4
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 908
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Try

String namestr = new String(name);
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:27 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC