Java 抛出错误。我怎样才能解决这个问题?I';我尝试过很多东西,但我是新手,逻辑有点超出我的理解

Java 抛出错误。我怎样才能解决这个问题?I';我尝试过很多东西,但我是新手,逻辑有点超出我的理解,java,exception,throw,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

带有“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);
            }
        }     
    }
}
import java.io.*;
导入java.util.*;
公共类PG2ERC
{    
私有静态ArrayList arl=new ArrayList();
private static BufferedReader br=new BufferedReader(new FileReader(“pageRefString.txt”);
公共静态void main(字符串[]args)引发IOException、FileNotFoundException
{
字符串[]arr;
int n1=3;
int n2=4;
int f;
int pf1=0;
int pf2=0;
arr=br.readLine().split(“”);
用于(字符串s:arr)
{
f=整数.parseInt(s);
如果(全部包含(f))
{
arl.remove(arl.indexOf(f));
arl.添加(f);
} 
else if(arl.size()
main
方法之外,将
BufferedReader
静态变量设置为null。并在
main
方法中初始化将修复该问题

private static BufferedReader br = null; 
    public static void main(String[] args) throws IOException, FileNotFoundException
    {
        br = new BufferedReader(new FileReader("pageRefString.txt"));

main
方法之外,将
BufferedReader
静态变量设置为null。并在
main
方法中初始化将修复该问题

private static BufferedReader br = null; 
    public static void main(String[] args) throws IOException, FileNotFoundException
    {
        br = new BufferedReader(new FileReader("pageRefString.txt"));

问题更简单地表现为:

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 ...
}

问题更简单地表现为:

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 ...
}
总是把问题简化成一个简单的问题。在本例中,主方法中的所有代码都不相关。你可以用几句话来演示整个过程。总是把问题简化成一个简单的问题。在本例中,主方法中的所有代码都不相关。你可以用几句话来演示整个过程。