Java Newbie question

M

M

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.
 
A

Andrew Thompson

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.
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..
(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..
 
Y

Yu SONG

M said:
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.
 
M

M

Thanks. I'll go thru the spec.

Yu SONG said:
M said:
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/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top