Java Newbie question

Discussion in 'Java' started by M, Jun 2, 2004.

  1. M

    M Guest

    Hi all, I'm learning Java 2 by myself and find question for help.

    I'm testing the System.in.read() method and tried the following program:

    import java.io.*;

    class ReadBytes {
    public static void main(String args[])
    throws IOException {
    byte data[] = new byte[10];

    System.out.println("Enter some characters.");
    System.in.read(data);
    System.out.print("You entered: ");
    for (int i=0; i<data.length; i++) {
    System.out.print((char)data);
    }
    }
    }

    My question are:
    (1) if I just comment out the "throws IOException" in the main, I
    just got compilation error! Is the any condition I can tell whether
    I have to throw something or not? I remembered in the HelloWOrld
    app, I don't have to throw anything.
    (2) If I entered more than 10 characters in the above test, I didn't
    get overflow error. Why? Actually I'm expected an exception to
    be caught.

    Thanks in advance.
    M, Jun 2, 2004
    #1
    1. Advertising

  2. On Wed, 2 Jun 2004 19:57:12 +0800, M wrote:

    > Hi all, I'm learning Java 2 by myself and find question for help.


    Please direct your quetions to
    c.l.j.help for the moment.
    <http://www.physci.org/codes/javafaq.jsp#cljh>

    > I'm testing the System.in.read() method and tried the following program:

    ....
    > My question are:
    > (1) if I just comment out the "throws IOException" in the main, I
    > just got compilation error! Is the any condition I can tell whether
    > I have to throw something or not?


    'JavaDocs'
    <http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#in>
    <http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#read()>

    The second link will most likey break because
    of the '()', find your way from here..
    <http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#method_summary>

    > (2) If I entered more than 10 characters in the above test, I didn't
    > get overflow error. Why?


    Your code only reads the size of the array, 10
    characters, then iterates through the existing 10.

    Try entering 5..

    --
    Andrew Thompson
    http://www.PhySci.org/ Open-source software suite
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.1point1C.org/ Science & Technology
    Andrew Thompson, Jun 2, 2004
    #2
    1. Advertising

  3. M

    Yu SONG Guest

    M wrote:
    > Hi all, I'm learning Java 2 by myself and find question for help.
    >
    > I'm testing the System.in.read() method and tried the following program:
    >
    > import java.io.*;
    >
    > class ReadBytes {
    > public static void main(String args[])
    > throws IOException {
    > byte data[] = new byte[10];
    >
    > System.out.println("Enter some characters.");
    > System.in.read(data);
    > System.out.print("You entered: ");
    > for (int i=0; i<data.length; i++) {
    > System.out.print((char)data);
    > }
    > }
    > }
    >
    > My question are:
    > (1) if I just comment out the "throws IOException" in the main, I
    > just got compilation error! Is the any condition I can tell whether
    > I have to throw something or not? I remembered in the HelloWOrld
    > app, I don't have to throw anything.
    > (2) If I entered more than 10 characters in the above test, I didn't
    > get overflow error. Why? Actually I'm expected an exception to
    > be caught.
    >
    > Thanks in advance.
    >


    Read this,
    http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#read(byte[])

    it says "public int read(byte[] b) throws IOException", so if you use
    it, you have to catch the thrown IOException.

    The above article also tells you when & why the method throws the exception.


    "Hello world" is here
    http://java.sun.com/j2se/1.4.2/docs/api/java/io/PrintStream.html#println(java.lang.String)

    It won't throw anything, so you don't need to.


    --
    Song

    More info.:
    http://www.dcs.warwick.ac.uk/~esubbn/
    Yu SONG, Jun 2, 2004
    #3
  4. M

    M Guest

    Thanks. I'll go thru the spec.

    "Yu SONG" <> wrote in message
    news:c9kh2o$df4$...
    > M wrote:
    > > Hi all, I'm learning Java 2 by myself and find question for help.
    > >
    > > I'm testing the System.in.read() method and tried the following program:
    > >
    > > import java.io.*;
    > >
    > > class ReadBytes {
    > > public static void main(String args[])
    > > throws IOException {
    > > byte data[] = new byte[10];
    > >
    > > System.out.println("Enter some characters.");
    > > System.in.read(data);
    > > System.out.print("You entered: ");
    > > for (int i=0; i<data.length; i++) {
    > > System.out.print((char)data);
    > > }
    > > }
    > > }
    > >
    > > My question are:
    > > (1) if I just comment out the "throws IOException" in the main, I
    > > just got compilation error! Is the any condition I can tell whether
    > > I have to throw something or not? I remembered in the HelloWOrld
    > > app, I don't have to throw anything.
    > > (2) If I entered more than 10 characters in the above test, I didn't
    > > get overflow error. Why? Actually I'm expected an exception to
    > > be caught.
    > >
    > > Thanks in advance.
    > >

    >
    > Read this,
    >

    http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#read(byte[]
    )
    >
    > it says "public int read(byte[] b) throws IOException", so if you use
    > it, you have to catch the thrown IOException.
    >
    > The above article also tells you when & why the method throws the

    exception.
    >
    >
    > "Hello world" is here
    >

    http://java.sun.com/j2se/1.4.2/docs/api/java/io/PrintStream.html#println(jav
    a.lang.String)
    >
    > It won't throw anything, so you don't need to.
    >
    >
    > --
    > Song
    >
    > More info.:
    > http://www.dcs.warwick.ac.uk/~esubbn/
    >
    M, Jun 2, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. peelman
    Replies:
    2
    Views:
    427
    anonymous
    Jan 13, 2005
  2. Replies:
    0
    Views:
    313
  3. Jerry C.
    Replies:
    8
    Views:
    228
    Uri Guttman
    Nov 23, 2003
  4. Kruno Saho
    Replies:
    0
    Views:
    134
    Kruno Saho
    Apr 7, 2013
  5. Dave Angel
    Replies:
    0
    Views:
    116
    Dave Angel
    Apr 7, 2013
Loading...

Share This Page