Class throws exception [closed]

2019-03-07 04:47发布

问题:

I am trying to refactor a large program in Netbeans and I am a bit lost. I haven't ever been very modular but am trying to correct that now, and for the future by actually learning how to do this. Unfortunately I am having trouble translating some of the tutorials to my beast of a program. SO I was hoping someone here could help. Currently I am trying to break off a chunk of code that takes a file of a specific format and makes a table. I know that I need to make a class and use it to create a table object but I am unsure how. I have a main that gets command line input for where a file is located:

public class Print {
  public static void main(String[] args) throws Exception {
            // I know this part works
     JSAP jsap = new JSAP();
     FlaggedOption opt3 = new FlaggedOption("cllmap")
            .setRequired(true)
            .setShortFlag('c')
            .setLongFlag("call map");
     opt3.setHelp("Where is the flu, map file? Full path");
     jsap.registerParameter(opt3);
     String cllmp = config.getString("map");

I haven't tried to reference any of the new classes I made yet because I have not figured out how to do it correctly yet. Then I try to send the location of the file to another class so that the other class can read in the file and parse it into a table object.

public class Reader extends Print {
 String inpt;

public class FReader {
    //reading in
 //throws exception error
    FileInputStream fstream = new FileInputStream(cllmp);
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    while ((inpt  = br.readLine()) != null) {
        if (!inpt.equals("Calls")) {
          ...

So why does my class FReader throw an exception and how to I make it stop? I can't say public class FReader throws Exception{ because I then get the error: '{' expected same with putting it after extends Fingerprint2. What am I doing wrong here? Any suggestions?

回答1:

Code must be put in methods, and not directly in the class body. The class is called Reader, so it should probably have a read() method, and this method should read, and throw an IOException if it can't do it:

public class Reader extends FingerPrint2 {
    public String read() throws IOException {
        // PUT YOUR CODE HERE
    }
}