java - Verify my understanding in this code. Check errors -


i have code:

   /*  * change license header, choose license headers in project properties.  * change template file, choose tools | templates  * , open template in editor.  */ package askisi5;  import java.io.*;  /**  *  * @author alexandros  */  public class io_tester {      public static int readint() {         byte b[] = new byte[16];         string str;           system.in.read(b);         str = (new string(b)).trim();         return integer.parseint(str);     } } 

as far understand code create function named readint() returns integer. inside function create byte array of 16 elements nad declare string variable str.

the next lines bit unclear me

system.in.read(b); --> input data program array b?

str = (new string(b)).trim(); --> seems trim leading , trailing whitespace string b. b converted string. save result str

return integer.parseint(str); --> return integer after have done type conversion?

why when compile piece of code gives error? error: unreported exception ioexception; must caught or declared thrown; system.in.read(b);

thanks

i quote java doc methods. java beautiful language apis (at least core language) being documented.

 system.in.read(b); 

reads number of bytes input stream , stores them buffer array b. number of bytes read returned integer. method blocks until input data available, end of file detected, or exception thrown.

if length of b zero, no bytes read , 0 returned; otherwise, there attempt read @ least 1 byte. if no byte available because stream @ end of file, value -1 returned; otherwise, @ least 1 byte read , stored b.

the first byte read stored element b[0], next 1 b[1], , on. number of bytes read is, @ most, equal length of b. let k number of bytes read; these bytes stored in elements b[0] through b[k-1], leaving elements b[k] through b[b.length-1] unaffected.

the read(b) method class inputstream has same effect as:
read(b, 0, b.length) parameters:b buffer data read.returns:the total number of bytes read buffer, or -1 if there no more data because end of stream has been reached.throws:ioexception - if first byte cannot read reason other end of file, if input stream has been closed, or if other i/o error occurs.nullpointerexception - if b null.

next trim() called on object of string. [new string(b) creates new string object decoding specified array of bytes using platform's default charset ]

a string value string, leading , trailing white space removed, or string if has no leading or trailing white space.

next integer.parseint(str) read below.

the integer value represented argument in decimal.


Comments