有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java对象编程中的文件错误

我需要人帮忙。我发现这个错误,文件无法解析为类型。这是在底部,上面写着File File=new File(文件名);不知道为什么。请有人帮忙

谢谢!

import java.util.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*; //step 1

    public class QuizPanel extends JPanel{
      private ArrayList<String>fileData = new ArrayList<String>();  
      private ArrayList<String>oneData = new ArrayList<String>(); 
      private ArrayList<String>twoData = new ArrayList<String>(); 

      private JButton submitButton;
      private JButton nextButton;
      private JButton levelOneButton;
      private JButton levelTwoButton;

      private JLabel question;
      private JLabel correctLabel = new JLabel("");
      private JLabel correctAnswer = new JLabel("");
      private JLabel ended = new JLabel("");
      private JLabel responce = new JLabel("");

     private JRadioButton [] options = { new JRadioButton (""), new JRadioButton (""), new JRadioButton (""), new JRadioButton (""),};
             ButtonGroup optionsGroup = new ButtonGroup ();

    private int questionNumber = 0;
    private int score = 0;

    //Border Layout Panels
      private JPanel northPanel = new JPanel();
      private JPanel eastPanel = new JPanel();
      private JPanel southPanel = new JPanel();
      private JPanel westPanel = new JPanel();
      private JPanel centerPanel = new JPanel();



      // constructor
      public QuizPanel(ArrayList<String>fileData){
        EventListener listener = new EventListener();//step 3
        this.fileData = fileData;

        question = new JLabel();

        submitButton = new JButton("Check Answer");
        submitButton.addActionListener(listener);//step 4

        nextButton = new JButton("Next Question");
        nextButton.addActionListener(listener);

        levelOneButton = new JButton("Level One");
        levelOneButton.addActionListener(listener);

        levelTwoButton = new JButton("Level Two");
        levelTwoButton.addActionListener(listener);

        for (JRadioButton each:options){
        optionsGroup.add(each);
        each.addActionListener(listener);
        centerPanel.add(each);
        }


        setLayout(new BorderLayout());

        //North
        northPanel.setPreferredSize(new Dimension(400,65));
        northPanel.setBackground(Color.black);
        northPanel.add(question);
        question.setVisible(false);
        question.setFont(new Font("Calibri",Font.BOLD,20));
        question.setForeground(Color.white);
        add(northPanel,BorderLayout.NORTH);

        //East
         eastPanel.setPreferredSize(new Dimension(100,200));
         eastPanel.setBackground(Color.green);
         eastPanel.add(submitButton);
         eastPanel.add(nextButton);
         nextButton.setVisible(false);
         add(eastPanel,BorderLayout.EAST);

        //South
         southPanel.setPreferredSize(new Dimension(400,65));
         southPanel.setBackground(Color.black);
         correctLabel.setFont(new Font("Calibri",Font.BOLD,20));
         correctLabel.setForeground(Color.white);
         southPanel.add(correctLabel);
         correctLabel.setVisible(false);

         ended.setFont(new Font("Calibri",Font.BOLD,20));
         ended.setForeground(Color.white);
         southPanel.add(ended);
         ended.setVisible(false);
         add(southPanel,BorderLayout.SOUTH);

        //West
        westPanel.setPreferredSize(new Dimension(100,200));
        westPanel.setBackground(Color.green);
        responce.setFont(new Font("Calibri",Font.BOLD,20));
        westPanel.add(responce);
        responce.setVisible(false);
        westPanel.add(levelOneButton);
        westPanel.add(levelTwoButton);
        add(westPanel,BorderLayout.WEST);

        //Center
         centerPanel.setBackground(Color.green);
         centerPanel.setVisible(false);
         add(centerPanel,BorderLayout.CENTER);



        //set up outer panel colour and size
        setPreferredSize(new Dimension(700,350));
        setBackground(Color.green);

        showQuestion(0);// show first question
      }

      //method for show Questions
      public void showQuestion(int questionNumber){
        String newQuestion = fileData.get(questionNumber);  
        System.out.println(newQuestion);

        String [] allQuestions = newQuestion.split("/");
        question.setText(allQuestions[0]);

        for(int i = 1; i < allQuestions.length - 1; i++){
          options[i-1].setText(allQuestions[i]);
        }    
         correctAnswer.setText(allQuestions[allQuestions.length-1]);
      } 


        // step 2 write an inner class
     private class EventListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
         String level = " ";

        if(e.getSource() == levelOneButton){
          String levelOne = "levelOne.txt";
          readFile(levelOne, oneData);
          fileData = oneData;
          showQuestion(0);
         } else if (e.getSource() == levelTwoButton){
           String levelTwo = "levelTwo.txt";
           readFile(levelTwo, twoData);
           fileData = twoData;
           showQuestion(0);
         }


         int clicked = 0;
         boolean answered = false;

         if(e.getSource() == submitButton){
           for(int i = 0; i < options.length; i++){
             if(options[i].isSelected()){
               clicked = i;
               answered = true;
           }
         }
           if(answered){
             if(correctAnswer.getText().equals(options[clicked].getText())){
               correctLabel.setText("Correct");
               nextButton.setVisible(true);
               correctLabel.setVisible(true);
               submitButton.setVisible(false);
               score++;
             }else{
               submitButton.setVisible(false);
               correctLabel.setVisible(true);
               nextButton.setVisible(true);
         String newQuestion = fileData.get(questionNumber); 
        String [] allQuestions = newQuestion.split("/");
        correctLabel.setText("Incorrect, Correct answer is " + allQuestions[5]);

         }  
        } 

         }else if (e.getSource() == nextButton){
           submitButton.setVisible(true);
           nextButton.setVisible(false);
           correctLabel.setVisible(false);

           if(questionNumber < fileData.size() - 1){ //next question Button
             questionNumber++;
             optionsGroup.clearSelection();
             correctAnswer.setText("");
             showQuestion(questionNumber); 

           }else{            

             submitButton.setVisible(false);
             nextButton.setVisible(false);
             question.setVisible(false);
             centerPanel.setVisible(false);
             ended.setVisible(true);
             ended.setText("Your score is " + score + "/3");
             responce.setVisible(true);
             responce.setText("Thank you for taking the quiz, Your score is below.");
         }
        }

       }

     }

      public static void readFile(String filename, ArrayList<String> fileData){
       File file = new File(filename);

       try{
          //instanitate a Scanner object to read from the object
          Scanner fileScan = new Scanner(file);
          //while there is another line yet to be read
          while(fileScan.hasNextLine()){
            fileData.add(fileScan.nextLine());
            //should manage exceptions - the String might not convert into a valid integer
          }
         }catch (FileNotFoundException e){
          System.out.println("File not found");
          System.exit(1); //exit from program if file not found
        }
      }
    }

共 (2) 个答案

  1. # 1 楼答案

    你想试试这个:-

    import java.io.*;
    

    你错过了import语句

  2. # 2 楼答案

    顶部缺少import语句

    import java.io.*;