Fri Sep 28 |
-
I miswrote the following lines:
InputStreamReader k = new InputStreamReader();
should be
InputStreamReader k = new InputStreamReader(System.in);
Also
public static void main(String[] args) {
should be
public static void main(String[] args) throws IOException{
I had this in my notes, but didn't pay enough attention to them when
I was writing on the board. Suffering from a cold does odd things to you...
-
The statement
InputStreamReader k = new InputStreamReader(System.in);
works as I said in class, except that the System.in argument
specifies where the input is coming from. In Java, the default is the
keyboard;
this can be changed using a method from the System class (setIn). The
default
should be fine for A3, and probably for the rest of the course.
-
The throws IOException is needed because 'bad' things can happen when
the user
is typing in information. For example, remember that I said that
BufferedReader
creates a buffer, or chunk of memory, into which characters are put.
What happens
if too much information is typed in and the buffer 'overflows'? Java
forces you
to deal with this through the mechanism of 'exception handling'. I'll
talk more
about this later. For now, just note that every method that calls
readLine()
needs to be declared with throws IOException. In addition, every
method that
calls a method that calls readLine() needs throws IOException, and so on.
The API lists readLine() as
public String readLine()
throws IOException
telling you that you need throws IOException in the method
declaration of the method that calls readLine()
My apologies; these two additions will make the code work
properly.
-
If you try to compile the code as I presented it in class, the compiler
will tell you that InputStreamReader doesn't exist.
The reason that the compiler insists that InputStreamReader doesn't
exist
is that we mistakenly called the constructor without an argument in the statement
InputStreamReader k = new InputStreamReader();
If you look in the API under package java.io, class InputStreamReader, there are two
constructors for the class InputStreamReader:
Constructor Summary:
InputStreamReader(InputStream in)
Create an InputStreamReader that uses the default character encoding.
InputStreamReader(InputStream in, String enc)
Create an InputStreamReader that uses the named character encoding.
Both of these declarations have at least one parameter; the compiler was looking
for a constructor declaration without a parameter and couldn't find one.
- The following is the correct code:
import java.io.*;
class Test{
private static BufferedReader in;
public static void main(String[] args) throws IOException{
InputStreamReader k = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(k);
String il = in.readLine();
System.out.println("il = " + il);
}
}
Of course, in needn't be declared as private static outside of main...
There are other possibilities, even for such a simple piece of code. You'll have to use
your knowledge of visibility to decide what's best in any particular application.
|
|