Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading File Error Handling

Status
Not open for further replies.

zcruz

Programmer
Oct 25, 2002
2
US
I don't know what's wrong with my code. It keeps on giving me 2 errors saying:

(line with try)
Error 1: llegal start of type

(line at the end of the catch block)
Error 2: <identifier> expected

Here's my code...
What did I do wrong?
-------------------------------
import java.io.*;
import java.util.StringTokenizer;
import java.util.Enumeration;
class pass_one
{
//Read in the file line by line
try
{
BufferedReader fsr = new BufferedReader( new FileReader(input));
String str;
while(( str = fsr.readline()) != null)
{
StringTokenizer st = new StringTokenizer(str);
while(st.hasMoreTokens())
{
String s = st.nextToken();
}
}

fsr.close() //close file
}

catch(IOException e)
{
System.err.println("Error reading file: " +e.toString());
}

}//end class pass_one

 
You've got some code floating in a class that isn't associated with a member function, so if you want to execute it,
you need to wrap it in a static block, like this:
static {
//stuff here.
}

This will execute the code once when the class is loaded.

That is probably not what you want. Try putting it in a method, if you use static, you don't create an instance, otherwise, create an instance:

class A{
//static method:
public static void do() {
}
}
class B{
//instance method:
public void do() {
}
}

To use your classes:
public static void main(String args[]){
A.do();
//and
new B().do;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top