抛出错误。我怎样才能解决这个问题?我尝试了很多东西,但我是新手,逻辑有点超出我的意义

时间:2016-11-03 07:48:42

标签: java exception throw

带有“private static BufferedReader”的行就是问题所在。 “-unreported exception java.io.FileNotFoundException;必须被捕获或声明被抛出”是错误。 这是我目前的代码:

import java.io.*;
import java.util.*;

public class PG2ERC
{    
    private static ArrayList<Integer> arl = new ArrayList<Integer>();
    private static BufferedReader br = new BufferedReader(new FileReader("pageRefString.txt"));
    public static void main(String[] args) throws IOException, FileNotFoundException
    {
        String [] arr; 
        int n1 = 3;
        int n2 = 4; 
        int f;
        int pf1 = 0;
        int pf2 = 0;
        arr = br.readLine().split(" ");
        for(String s:arr)
        {
            f=Integer.parseInt(s);
            if(arl.contains(f)) 
            {
                arl.remove(arl.indexOf(f));
                arl.add(f);
            } 
            else if(arl.size() < n1) 
            {
                ++pf1;
                arl.add(f);
            } 
            else 
            {
                arl.remove(0);
                arl.add(f);
                ++pf1;
            }


            f=Integer.parseInt(s);
            if(arl.contains(f)) 
            {
                arl.remove(arl.indexOf(f));
                arl.add(f);
            } 
            else if(arl.size() < n2) 
            {
                ++pf2;
                arl.add(f);
            } 
            else 
            {
                arl.remove(0);
                arl.add(f);
                ++pf2;
            }
            try (Writer writer = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream("pg2out.txt"))))
            {
                writer.write(("Number of page faults is ") + pf1);
                writer.write(("Number of page faults is ") + pf2);
            }
        }     
    }
}

2 个答案:

答案 0 :(得分:2)

问题更简单地证明了这一点:

class DownloadTask extends AsyncTask <String, Integer, MyDownloadedData> {

    private MyDownloadedData getFromStream(InputStream stream){
        // TODO
        return null;
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected MyDownloadedData doInBackground(String... params){

        try {
            URL url = new URL(url);
            InputStream stream = url.openStream();

            MyDownloadedData data = getFromStream(stream);
            return data;
        } catch (Exception e) {
            // do something
        }
        return stream;
    }

    @Override
    protected void onPostExecute(MyDownloadedData data) {
        //
        // update your UI in this method. example:
        //
        someLabel.setText(data.getName());
    }

    protected void onProgressUpdate(Integer... progress) {
        //...
    }


}

问题是您的类的静态初始化程序可能会抛出异常。这与您的import java.io.*; class Test { private static Reader reader = new FileReader("foo.txt"); } 方法完全分开。

现在,在您的情况下,最简单的解决方案是将字段更改为本地变量:

main

此时,可以抛出异常的代码位于一个声明可以抛出这些异常的方法中,所以很好。

如果你需要像这样初始化静态变量,你应该在静态初始化块中这样做:

// No need to declare FileNotFoundException - it's a subclass of IOException anyway
public static void main(String[] args) throws IOException
{
    ArrayList<Integer> arl = new ArrayList<Integer>();
    BufferedReader br = new BufferedReader(new FileReader("pageRefString.txt"));
    ... rest of method as before ...
}

答案 1 :(得分:0)

var Form = React.createClass({ getInitialState: function() { return { value: 12.12 }; }, handleChange: function(e) { this.setState({ value: e.target.value }); }, render: function() { return ( <input onChange = {(e) => this.handleChange(e)} type = "number" value = {this.state.value} /> ); } }); ReactDOM.render( <Form /> , document.getElementById('container')); 方法之外将BufferedReader静态变量设置为null。并在main方法中初始化将解决问题。

main