简体   繁体   English

抛出错误。 我怎样才能解决这个问题? 我尝试了很多事情,但是我是新手,逻辑超出了我的范围

[英]Throw error. How can I fix this? I've tried many things, but I am new to it and the logic is a little beyond me

The line with "private static BufferedReader" is where the problem lies. 问题所在是带有“ private static BufferedReader”的行。 "-unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown" is the error. 错误是“-未报告的异常java.io.FileNotFoundException;必须捕获或声明为抛出”。 Here is my current code: 这是我当前的代码:

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);
            }
        }     
    }
}

The problem is more simply demonstrated like this: 该问题更简单地显示为:

import java.io.*;

class Test {
    private static Reader reader = new FileReader("foo.txt");
}

The problem is that the static initializer for your class can throw an exception. 问题是您的类的静态初始化器可能引发异常。 That's entirely separate to your main method. 这与您的main方法完全分开。

Now in your case, the simplest solution is to change your fields to local variables: 现在,根据您的情况,最简单的解决方案是将字段更改为局部变量:

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

At that point, the code that can throw an exception is within a method that declares that those exceptions can be thrown, so it's fine. 那时,可以引发异常的代码在声明可以引发这些异常的方法之内,所以很好。

If you do need to initialize static variables like this, you should do so in a static initializer block: 如果确实需要像这样初始化静态变量,则应在静态初始化程序块中进行初始化:

private static BufferedReader br;
static {
    try { 
        br = new BufferedReader(new FileReader("pageRefString.txt"));
    } catch (IOException e) {
        // Go bang hard - RuntimeException isn't a checked exception
        throw new RuntimeException(e);
    }
}

Set your BufferedReader static variable to null outside the main method. main方法之外,将BufferedReader静态变量设置为null。 and initialize in main method will fix the issue. 并在main方法中初始化将解决此问题。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我得到错误:可靠的精度损失,我试图改变它,但我可以解决它,请帮助我 - I get the error: posible loss of of precision and i have tried to change it but I can fix it, please help me 启动 Inteliji IDEA 会出错。 我该如何解决? - Launching Inteliji IDEA gives an error. How can I fix that? PagerAdapter 是抽象的; 无法实例化错误。 我该如何解决? - PagerAdapter is abstract; cannot be instantiated error. How can i fix it? 在Android Studio中添加解析sdk后出现错误。 我在互联网上尝试了许多解决方案,但似乎没有什么可以解决 - Got an error after adding parse sdk in android studio. I've tried many solution on internet but seems like nothing can solve 我尝试编写 n 个皇后问题,我是初学者,我认为主要逻辑是正确的,但我没有按要求得到 output。有人可以帮我吗? - I tried to code n queens problem, I'm a beginer and I think that main logic is right but I am not getting output as required.can someone help me out? 如何在不使用我给我的 servlet 的新名称的情况下修复 Tomcat? - How can I fix Tomcat not using the new names I've given my servlets? 我正在尝试在 Java 中呈现一个蓝色方块,但出现 IllegalStateException 错误。 有人能帮我吗? - I'm Trying To Render A Blue Square In Java But I Am Getting A IllegalStateException Error. Can Someone Help Me? 这段代码给我一个不兼容的类型错误。 它说它需要一个扫描器,但是找到了字符串。 我不明白该如何解决 - This code gives me an incompatible types error. It says it requires a Scanner but found String. I don't understnd how I can fix it 我如何才能无例外地修复此逻辑语句错误? - How can I fix this logic sentence error without exceptions? LibGDX 一直给我这个错误:“ERROR BAD LOGIC GAMES”,我该如何解决这个问题? - LibGDX keeps giving me this error: “ERROR BAD LOGIC GAMES”, how do I fix this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM