简体   繁体   English

在actionPerformed(ActionEvent event)方法中抛出IOException

[英]Throw IOException in actionPerformed(ActionEvent event) method

I am trying to create a guessing game program for my online course, where there are a bunch of questions and you are able to answer them, but I have come to the point in my program where I am having some difficulties... 我正在尝试为我的在线课程创建一个猜谜游戏程序,那里有很多问题,您可以回答,但是我的程序中有些困难……

Now, what I am trying to do is read a file from an "actionPerformed(ActionEvent event)" method, then display if the users answer is right... 现在,我要尝试的是从“ actionPerformed(ActionEvent事件)”方法中读取文件,然后显示用户答案是否正确...

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

Now I dunno what I have done wrong, but I am trying to just make it so that when a user types their answer into the box, and you hit enter, it scans the file to see if their answer is right, then displays if its right or wrong... 现在我不知道我做错了什么,但是我试图做到这一点,以便当用户在框中输入答案时,按回车键,它将扫描文件以查看其答案是否正确,然后显示是否正确对还是错...

Hope this explains it a bit. 希望这能解释一下。 Thanks for the help. 谢谢您的帮助。

Your file answers_file.txt is not being found. answers_file.txt您的文件answers_file.txt Add the following line of debug code to see where your program is actually expecting the file to be. 添加以下调试代码行,以查看程序实际期望文件所在的位置。

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

Also look very closely at the spelling of the filename in the code versus the actual filename on disk. 还要仔细查看代码中文件名的拼写与磁盘上实际文件名的拼写。

And remember backslash \\ is the String escape character in Java, so if you use backslashes in a file path you have to escape it like this: 并记住反斜杠\\是Java中的String转义字符,因此,如果在文件路径中使用反斜杠,则必须像这样对它进行转义:

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

Finally, if you're still having the problem, add this line of debug code to your catch block so you can see the exact error message: 最后,如果仍然存在问题,请将以下调试代码行添加到catch块中,以便您可以看到确切的错误消息:

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

With regard to throwing IOException from ActionListener.actionPerformed() - that is not allowed because IOException is a checked exception and is not declared to be thrown in the interface method declaration. 关于从ActionListener.actionPerformed()抛出IOException ,这是不允许的,因为IOException是一个已检查的异常 ,并且没有声明要在接口方法声明中抛出。

暂无
暂无

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

相关问题 “必须实现继承的抽象方法java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)”是什么意思? - what does “must implement the inherited abstract method java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)” mean? java:按钮不是抽象的,并且不会覆盖 java.awt.event.ActionListener 中的抽象方法 actionPerformed(java.awt.event.ActionEvent) - java: Button is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener 无法传递在“ public void actionPerformed(ActionEvent事件)”中声明的变量 - Variable delclared in “public void actionPerformed(ActionEvent event)” is cannot be passed 得到错误:BodyMassApplet不是抽象的,并且不会覆盖抽象方法actionPerformed(ActionEvent) - Get error: BodyMassApplet is not abstract and does not override abstract method actionPerformed(ActionEvent) 类不是抽象的,并且不覆盖抽象方法actionPerformed(ActionEvent) - Class is not abstract and does not override abstract method actionPerformed(ActionEvent) AListener不是抽象的,并且不会覆盖ActionListener中的抽象方法actionPerformed(ActionEvent) - AListener is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener 类不是抽象的,并且不会重写ActionListener中的抽象方法actionPerformed(ActionEvent) - Class is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener Select不是抽象的,并且不会覆盖ActionListener中的抽象方法actionPerformed(ActionEvent) - Select is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener ExitButtonHandler不是抽象的,并且不会重写ActionListener中的抽象方法actionPerformed(ActionEvent) - ExitButtonHandler is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener 关于ActionPerformed和ActionEvent - About ActionPerformed and ActionEvent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM