Getting Illegal start of type on try block
Any Java sentence must be inside a method. This code is not.
The fact that BufferedWriter wr = new BufferedWriter(new FileWriter("target.txt"));
works is because is declared as a default field (no scope mark was given) in your file_interface
class and is being initialized. It is similar to do:
public class file_interface {
BufferedWriter wr;
public file_interface() {
wr = new BufferedWriter(new FileWriter("target.txt"));
}
}
Just create a method to hold your logic:
public class file_interface {
public void foo() {
//your code goes here...
//now this is not a field but a variable in the method
BufferedWriter wr = new BufferedWriter(new FileWriter("target.txt"));
BufferedReader br = new BufferedReader(new FileReader("Assign4.txt"));
//rest of your code...
try {
//...
} catch (...) {
//...
}
//...
}
}
Then just call your method in your client class. For example, a class with the main
method:
public class AMainClass {
public static void main(String[] args) {
file_interface fi = new file_interface();
fi.foo();
}
}
Another example, a class with another method:
public class SomeClientClass {
public void bar() {
file_interface fi = new file_interface();
fi.foo();
}
}
Note: You should also follow the Java Naming Conventions, so you should rename file_interface
by FileInterface
or even MyFileUtil
since interface
word sounds more to declare an, uhm, interface
.
Taylor
I am a Computer Science student studying in Denver, Colorado at University of Colorado, Denver Campus. Currently I am working on a few small projects to learn more about coding in general, and to help me have more to show for my Degree once I graduate.
Updated on June 13, 2022Comments
-
Taylor almost 2 years
So Im trying to get a basic reader going so that I can work with files for an authentication process later.
The problem I am having is that I get an error on my BufferedReader line that causes my try function to throw an illegal start exception and it wont run. Eclipse is showing me an error on the semicolon at the end of the br declaration and says I should be putting a { but I can't see why that would be neccessary.
BufferedReader br = new BufferedReader(new FileReader("Assign4.txt"));
I have tried to put that there but it breaks the entire try section.
package main; import java.io.*; public class file_interface { BufferedWriter wr = new BufferedWriter(new FileWriter("target.txt")); BufferedReader br = new BufferedReader(new FileReader("Assign4.txt")); try { int count = 1; StringBuilder sb = new StringBuilder(); String line = br.readLine(); while (line != null) { sb.append(count++); sb.append(line); sb.append("\n"); wr.write(line); line = br.readLine(); } } catch (IOException e) { System.err.println("Error: " + e); } finally { br.close(); wr.close(); } } }
-
atk over 10 yearsOr inside a static block.
-
Pshemo over 10 yearsMaybe instead of
foo
better suggestion would bemain
method (unlessfoo
returns something). Anyway +1. -
Luiggi Mendoza over 10 years@atk that would be great if you want to shoot at your feet.
-
atk over 10 years@LuiggiMendoza: non-sequitor. Java allows you to write code in static blocks, so that you can do stuff when loading a class definition. You may personally dislike static blocks, but they definitely have a use.
-
Luiggi Mendoza over 10 years@atk the fact that you can doesn't mean you should.
-
atk over 10 years@LuiggiMendoza: do you have an actual objection to using static blocks? Or do you simply prefer tilting at windmills and strawmen? I never said that they are useful for all purposes, but that they do have their uses.
-
Radiodef over 10 yearsA static block would not be an appropriate solution to the OP's application which is basically copying a file. A static block is a great thing you can do sometimes but not an answer to this question.
-
Taylor over 10 yearsYeah that made sense, it was the fact that the first one didn't throw an error that messed me up. Thanks
-
Luiggi Mendoza over 10 years@atk I have no need to answer to your comment when there's a Q/A here that solves this: Java: when to use static methods.
-
atk over 10 years@LuiggiMendoza: thank you for your answer to my comment as well as the link to the discussion :)