New to java questions

#1. In the code below if I uncomment the try/catch surrounding the

BufferedReader I get a compile error stating "variable br might not have

been initialized"...any ideas why?

#2. In order to get this code to run I had to import FileReader.

Shouldn't FileReader have been imported with java.io.* ?

Thanks in advance. Jim

import java.io.*;

import java.io.FileReader;

class FileReaderDemo {

public static void main(String args[]) throws IOException {

String s;

FileReaderfr;

BufferedReader br;

try {

fr = new FileReader(args[0]);

} catch(ArrayIndexOutOfBoundsException e) {

System.out.println("Usage: Java FileReaderDemo file name");

return;

} catch (FileNotFoundException e) {

System.out.println("File not found");

return;

}

//try {

br = new BufferedReader(fr,100);

//} catch (IllegalArgumentException e) {

//System.out.println("Error: " + e);

//}

try {

while((s = br.readLine()) != null) {

System.out.println(s);

}

} catch (Exception e) {

System.out.println("Error: " + e);

}

fr.close();

}

}

[1218 byte] By [j_wyanta] at [2007-10-2 0:34:20]
# 1

> #1. In the code below if I uncomment the try/catch

> surrounding the

> BufferedReader I get a compile error stating

> "variable br might not have

> been initialized"...any ideas why?

Beacause the variable might not have been initialized.

> #2. In order to get this code to run I had to import

> FileReader.

> Shouldn't FileReader have been imported with

> java.io.* ?

Only java.io.* will be enough.

This will compile(I think):

import java.io.*;

class FileReaderDemo {

public static void main(String args[]) throws IOException {

String s = "";

FileReader fr = null;

BufferedReader br = null;

try {

fr = new FileReader(args[0]);

} catch(ArrayIndexOutOfBoundsException e) {

System.out.println("Usage: Java FileReaderDemo file name");

return;

} catch (FileNotFoundException e) {

System.out.println("File not found");

return;

}

try {

br = new BufferedReader(fr,100);

} catch (IllegalArgumentException e) {

System.out.println("Error: " + e);

}

try {

while((s = br.readLine()) != null) {

System.out.println(s);

}

} catch (Exception e) {

System.out.println("Error: " + e);

}

fr.close();

}

}

prometheuzza at 2007-7-15 16:48:53 > top of Java-index,Java Essentials,New To Java...
# 2

> #1. In the code below if I uncomment the try/catch

> surrounding the

> BufferedReader I get a compile error stating

> "variable br might not have

> been initialized"...any ideas why?

If the BufferedReader's constructor has thrown an IllegalArgumentException

your code would have caught it, printed a message and continued,

in particular you would have attempted to apply readLine to an uninitialized br.

Perhaps you have a missing return here.

>

> #2. In order to get this code to run I had to import

> FileReader.

> Shouldn't FileReader have been imported with

> java.io.* ?

That's odd. Do you have a FileReader class accidentally defined in your directory?

DrLaszloJamfa at 2007-7-15 16:48:53 > top of Java-index,Java Essentials,New To Java...
# 3
Thanks DrLaszloJamf,I did have a FileReader class from an earlier exercise. My mistake, thanks for the help.
j_wyanta at 2007-7-15 16:48:53 > top of Java-index,Java Essentials,New To Java...
# 4
Thank you prometheuzz . It's working now.
j_wyanta at 2007-7-15 16:48:53 > top of Java-index,Java Essentials,New To Java...