【问题标题】:Throw IOException in actionPerformed(ActionEvent event) method在 actionPerformed(ActionEvent event) 方法中抛出 IOException
【发布时间】:2015-01-16 19:00:45
【问题描述】:

我正在尝试为我的在线课程创建一个猜谜游戏程序,其中有很多问题,您可以回答它们,但是我在程序中遇到了一些困难。 .

现在,我要做的是从“actionPerformed(ActionEvent event)”方法中读取文件,然后显示用户回答是否正确...

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;
public class allQuestions implements ActionListener
{
    public void actionPerformed(ActionEvent event) 
    {
        if(event.getActionCommand().equals("Answer M 100"))
        {
            try
            {
                File answersFile = new File("answers_file.txt");
                Scanner sc = new Scanner(answersFile);
                if(sc.hasNext("1000000"))
                {
                    System.out.println("Your answer is correct!");
                }
                else
                {
                    System.out.println("Your answer is wrong.");
                }
            }
            catch(IOException e)
            {
                System.out.println("File cannot load.");
            }
        }
    }


    public static void math100()
    {
        JFrame m100Frame = new JFrame("100 Point Math Question");
        m100Frame.setSize(350,350);
        m100Frame.setLocationRelativeTo(null);
        JPanel pane = new JPanel();
        m100Frame.setContentPane(pane);

        JLabel question = new JLabel("<html><p><div WIDTH = 320><center>Round 1,000,203 to the nearest thousands, and round 8.472 to the nearest hundredth.</p><p>Put answers in box below, and have the word 'and' between the two answers.</center></width></div></html>");

        JTextArea answer = new JTextArea("Enter Answers Here", 10, 25);

        JButton answerQuestion = new JButton("Answer M 100");

        answerQuestion.addActionListener(new allQuestions());

        pane.add(question);
        pane.add(answer);
        pane.add(answerQuestion);

        m100Frame.setVisible(true);
        m100Frame.toFront();
    }
}

现在我不知道我做错了什么,但我试图做到这一点,以便当用户在框中输入他们的答案并按 Enter 键时,它会扫描文件以查看他们的答案是否正确,然后显示正确或错误...

希望这能解释一下。感谢您的帮助。

【问题讨论】:

  • IOException = 文件有问题。它存在吗?在哪个文件夹中?
  • 你应该用java标记问题,而不是javascript
  • Theirry,我的文件夹中有它需要的文件,但我不断收到错误消息说它需要抛出 IOException,但它不允许我将它添加到 actionPerformed方法...
  • 你是在编译,还是在线课程'bot'告诉你的?
  • 它告诉我编译它。真的很难解释。就像我想创建一个教育猜谜游戏的应用程序一样,所以我正在制作一个游戏,您可以在其中单击特定问题的特定按钮,当您单击特定问题时,假设会显示问题和文本框。在文本框中,您应该回答问题,然后点击按钮回答问题。然后它会转到我创建的文件并搜索您输入的答案,然后显示答案是否正确。但它不适合我。

标签: java


【解决方案1】:

找不到您的文件answers_file.txt。添加以下调试代码行以查看您的程序实际期望文件的位置。

File answersFile = new File("answers_file.txt"); // existing line
System.out.println(answersFile.getAbsolutePath()); // new debugging line

还要仔细查看代码中文件名的拼写与磁盘上的实际文件名。

记住反斜杠\是Java中的字符串转义字符,所以如果你在文件路径中使用反斜杠,你必须像这样转义它:

String path = "C:\\folder\\file.txt";

最后,如果您仍然遇到问题,请将这行调试代码添加到您的 catch 块中,以便您可以看到确切的错误消息:

        catch(IOException e)
        {
            System.out.println("File cannot load."); // existing line
            e.printStackTrace(); // new debugging line
        }

关于从ActionListener.actionPerformed() 抛出IOException - 这是不允许的,因为IOException 是一个已检查异常,并且没有在接口方法声明中声明要抛出。

【讨论】:

  • 嗯......似乎没有用......就像我想做的那样,让 JTextArea 读取用户输入的数字,然后他们点击一个按钮即在该帧上,然后将其发送到 actionPerformed 方法以检查并查看用户输入的数字是否在文件中,如果是,则显示输入的答案是正确的,但我无法得到即将发生。每次我尝试完成这项工作时,它都会说找不到文件...
  • 我已经回答了你的问题“在 actionPerformed(ActionEvent event) 方法中抛出 IOException”。如果您想要更好的答案,请参阅How to ask。您正在让您的程序打印字符串“文件无法加载”。对于每个可能的 IOException,当这可能不是正确的解释时。对于错误背后的真正原因,请在您的问题中包含完整的异常堆栈跟踪。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-29
  • 2012-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多