Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java Newbie question

Reply
Thread Tools

Java Newbie question

 
 
M
Guest
Posts: n/a
 
      06-02-2004
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[i]);
}
}
}

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.



 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      06-02-2004
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
 
Reply With Quote
 
 
 
 
Yu SONG
Guest
Posts: n/a
 
      06-02-2004
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[i]);
> }
> }
> }
>
> 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/...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/...va.lang.String)

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


--
Song

More info.:
http://www.dcs.warwick.ac.uk/~esubbn/

 
Reply With Quote
 
M
Guest
Posts: n/a
 
      06-02-2004
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[i]);
> > }
> > }
> > }
> >
> > 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/...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/...ml#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/
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java newbie, error in java or in squid proxy setup? pantagruel Java 1 03-10-2008 03:30 PM
Java Newbie question about Java POI sandejones@gmail.com Java 0 04-27-2007 11:55 AM
newbie q: .java file and java project difference xarora Java 5 07-28-2005 05:22 AM
Java Newbie Question - Java on Windows Server 2003 peelman Java 2 01-13-2005 10:55 PM
dumb newbie question (or newbie dumb question) Jerry C. Perl Misc 8 11-23-2003 04:11 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57