new use of bufferedreader, uncharted territory
I was trying to write part of a program to read in lines from a text file (in the same directory as the .java and compiled class) and print out those lines. I know a little java, but not much, and I don't have the expertise to figure out what is wrong with this code.
Code java:
import java.io.*;
public class Prog1{
public static void main(String args[]){
try {
BufferedReader in = new BufferedReader(new FileReader("testtext.txt"));
String line = in.readLine();
in.close();
} catch (FileNotFoundException ex) { Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex); }
if(line==null){
System.out.println("reading process done");
} else {
System.out.println(line);
}
}
}
Re: new use of bufferedreader, uncharted territory
Why do you think there is anything wrong with the code?
Can you explain?
Re: new use of bufferedreader, uncharted territory
Sorry, don't know how this works, there were some compile errors with the symbols Level and Logger both times they appeared. At one point, I tried defining those variables but that did not work either, then it assumed that SEVERE was a variable.
Re: new use of bufferedreader, uncharted territory
Quote:
there were some compile errors
Please copy and paste the full text of the errors here if you want help with them.
Why are you using Level and Logger?
Re: new use of bufferedreader, uncharted territory
Here were the compiler errors:
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 9]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:9: cannot find symbol
symbol : variable Logger
location: class Prog1
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 9]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:9: cannot find symbol
symbol : variable Level
location: class Prog1
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 9]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:9: cannot find symbol
symbol : variable Logger
location: class Prog1
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 9]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:9: cannot find symbol
symbol : variable Level
location: class Prog1
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 11]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:11: cannot find symbol
symbol : variable line
location: class Prog1
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 14]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:14: cannot find symbol
symbol : variable line
location: class Prog1
The unfamiliar classes come from the logger class java.util.logging.Logger
void log(Level level, String msg, Throwable thrown)
Log a message, with associated Throwable information.
static Logger getLogger(String name)
Find or create a logger for a named subsystem.
log(Level level, String msg, Throwable thrown)
Log a message, with associated Throwable information.
Re: new use of bufferedreader, uncharted territory
You need to tell the compiler where to find the definitions for those classes. You can use an import statement to do that.
Re: new use of bufferedreader, uncharted territory
so I tried some changes and commented out my print statements near the end (since that's a separate mess there):
Code java:
import java.io.*;
import java.util.logging.Logger;
public class Prog1{
public static void main(String args[]){
try {
BufferedReader in = new BufferedReader(new FileReader("testtext.txt"));
String line = in.readLine();
in.close();
} catch (FileNotFoundException ex) { Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex); }
// if(line==null){
// System.out.println("reading process done");
// } else {
// System.out.println(line);
}
}
}
That yielded only one error (also, it no longer had a problem identifying Logger). This was the error:
#Compilation completed. The following files were not compiled:
1 error found:
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 18]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:18: class, interface, or enum expected
Re: new use of bufferedreader, uncharted territory
What is on or before line 18?
Make sure the {}s are correctly paired.
Re: new use of bufferedreader, uncharted territory
Re: new use of bufferedreader, uncharted territory
Quote:
is it appropriate for me to ask this many questions?
Definitely, yes.
Where is the Level variable defined? If it is from the Java SE classes, read the API doc to find out what package it is in and add an import for it.
Re: new use of bufferedreader, uncharted territory
That worked, and the program compiled. I found a way to modify the program further, supposedly so it can print its inputs from the file it reads. That created some additional challenges. Earlier I had those system.out.println statements that I called a "mess," but commented out so the program would compile.
Code java:
import java.io.*;
import java.util.logging.Logger;
import java.util.logging.*;
public class Prog1{
public static void main(String args[]){
File file = new File("test.txt");
StringBuffer contents = new StringBuffer();
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String line = null;
while ((line = in.readLine()) != null) {
contents.append(line)
.append(System.getProperty(
"line.separator"));
} catch (FileNotFoundException ex) {
Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(contents.toString());
}
}
}}
I got ten errors this time, it looks like all of the errors are related.
10 errors found:
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 10]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:10: 'try' without 'catch' or 'finally'
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 17]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:17: 'catch' without 'try'
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 17]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:17: not a statement
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 17]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:17: ')' expected
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 17]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:17: ';' expected
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 19]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:19: 'catch' without 'try'
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 19]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:19: not a statement
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 19]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:19: ')' expected
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 19]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:19: ';' expected
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 21]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:21: 'finally' without 'try' |
Re: new use of bufferedreader, uncharted territory
Again check for correctly matching {}s
Re: new use of bufferedreader, uncharted territory
Thanks, that worked and I've also learned a lot about how to read the compiler errors too.
Code java:
import java.io.*;
import java.util.logging.Logger;
import java.util.logging.*;
public class Prog1
{
public static void main(String args[])
{
File file = new File("test.txt");
StringBuffer contents = new StringBuffer();
try {
//first block of inserted code
BufferedReader in = new BufferedReader(new FileReader(file));
String line = null;
while ((line = in.readLine()) != null) { contents.append(line).append(System.getProperty("line.separator")); }
//end first block of inserted code
} catch (FileNotFoundException ex) {
Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) { Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex);
} finally
{
try {
if (in != null){ in.close(); } //block of insered code
} catch (IOException ex) {
Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println(contents.toString());
} //end main method
}//end class
So this time it doesn't seem to be an import related problem, an object used earlier in the code is not recognized twice, although it does exist within a nested try-catch within the main one where the instance of the object was created.
2 errors found:
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 24]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:24: cannot find symbol
symbol : variable in
location: class Prog1
File: /Volumes/A-H/ajs211/Prog1/Prog1.java [line: 24]
Error: /Volumes/A-H/ajs211/Prog1/Prog1.java:24: cannot find symbol
symbol : variable in
location: class Prog1
Re: new use of bufferedreader, uncharted territory
The problem is the scope of the definition for the variable. The definition is only known within the enclosing pair of {}s. You need to move the definition up until it is at the same nesting level of {}s where you are trying to use it. You can define it in one place (and give it a null value) and give it a value in another place.
Re: new use of bufferedreader, uncharted territory
At this point it compiled fine, but no values seem to be passed to the println statement at the bottom of the class since the console isn't printing the values in the text file to the console.
Code java:
import java.io.*;
import java.util.logging.Logger;
import java.util.logging.*;
public class Prog1
{
public static void main(String args[])
{
File file = new File("test.txt");
StringBuffer contents = new StringBuffer();
BufferedReader in = null;
String line = null;
try {
//first block of inserted code
in = new BufferedReader(new FileReader(file));
while ((line = in.readLine()) != null) { contents.append(line).append(System.getProperty("line.separator")); }
//end first block of inserted code
} catch (FileNotFoundException ex) {
Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) { Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex);
} finally
{
try {
if (in != null){ in.close(); } //block of insered code
} catch (IOException ex) {
Logger.getLogger(Prog1.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println(contents.toString());
} //end main method
}//end class
Re: new use of bufferedreader, uncharted territory
Add more printlns to show the values of the variables as they are changed. For example print out the value of line immediately after each readLine() statement.