CSC108 UT St. George Section L0203 
Fall Term 2001

Errata

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.
 
Fri Oct 26
  • The statement I wrote to instantiate an array of objects on the board today was:
    Student [] studentSet = new Student()[1000];
    
    As Mike was indicating, (my apologies for not catching on!) this is incorrect. It should be:
    Student [] studentSet = new Student[1000];
    
    The () brackets do not appear in the correct syntax. As usual the following lines do the same thing:
    Student [] studentSet;
    studentSet = new Student[1000];
    
    This does not allocate the Student objects however. It merely allocates memory for the references to the objects. The correct way to actually allocate the objects would look like something like:
    for (int i = 0; i < 1000; i++){
            studentSet[i] =  new Student();
    }
    
    Here we instantiate 1000 Student objects, and store the references to them in the memory locations that we allocated with the statement studentSet = new Student[1000]; See the notes on arrays for more details.