0

当我尝试编译时,我不断收到此消息:解析时到达文件末尾有人知道为什么吗?我唯一想到的是它与我的花括号有关。我试图移动大括号,添加它们并删除它们,但我无法弄清楚这一点。错误发生在最后一行代码中。

import java.awt.event.ActionEvent; //Next group of lines import various Java classes
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import java.text.*;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class ReadTextFile extends JFrame
{
public static void main(String[] args) throws IOException {
    //Creates Window
    final JFrame frame = new JFrame();
    frame.setSize(450, 300); //Sets size of the window
    frame.setTitle("Read a Text File"); //Adds title to the GUI
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Create labels and fields
    JLabel Firstlbl = new JLabel("First Text Line");
    final JTextField FirstField = new JTextField(20);
    FirstField.setText("1st");
    JLabel Secondlbl = new JLabel("Second Text Line");
    final JTextField SecondField = new JTextField(20);
    SecondField.setText("2nd");
    JLabel Thirdlbl = new JLabel("Third Text Line");
    final JTextField ThirdField = new JTextField(20);
    ThirdField.setText("3rd");
    JLabel ButtonLabel = new JLabel("Click button to read text from file.");
    final JButton button = new JButton("Click Here");
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(4,2));
    panel.add(ButtonLabel);
    panel.add(button);
    panel.add(Firstlbl);
    panel.add(FirstField);
    panel.add(Secondlbl);
    panel.add(SecondField);
    panel.add(Thirdlbl);
    panel.add(ThirdField);
    frame.add(panel);
    class CalculateListener implements ActionListener {

        private boolean readFile(String fileName)
{
    boolean flag = true;

    try{

        //initialize the file object
        BufferedReader reader=new BufferedReader(new FileReader(fileName));             
        String line;
        int counter = 0;
        //reading the lines
        while((line = reader.readLine()) != null)
        {
            counter++;              
        }        
        reader.close();

        //if file has less then 6 line
        if(counter<6)
        {   
            //return the message
            JOptionPane.showMessageDialog(this, "Error: This must have minimum 6 lines\nEnter another file name and try again.", "FILE LINES ERROR", JOptionPane.ERROR_MESSAGE);
            flag = false;       
        }

        if(flag){

            //initialize the array wtih line counter
            lines = new String [counter];
            reader=new BufferedReader(new FileReader(fileName));                
            //reading the lines
            counter =0;
            while((line = reader.readLine()) != null)
            {
                //set the array elements with the lines of the file
                lines[counter++] = line;                
            }        
            reader.close();

        }

    }
    catch(IOException ioe) //exception if any
    {
        JOptionPane.showMessageDialog(this, "Error"+ioe.getMessage(), "FILE READING ERROR", JOptionPane.ERROR_MESSAGE);         
    }
    catch(Exception e) //exception if any
    {
        JOptionPane.showMessageDialog(this, "Error"+e.getMessage(), "GENERAL ERROR", JOptionPane.ERROR_MESSAGE);            
    }

    return flag;

}

//method to handle action of button
public void actionPerformed (ActionEvent ae)
{
    if(ae.getSource()== displayButton)
    {
        resultTextArea.setText("");
        String fileName = "input.txt";

        //call the function readFile() with file name
        if(readFile(fileName))
        {
            for(int i=0; i< lines.length; i++)
            {
                if(i%2==0)
                {
                    //display the array elements to text area
                    resultTextArea.append(lines[i]+"\n");

                }
            }

        }

    }
}       
}

}

4

2 回答 2

3

是的,你没有足够的右括号。

一个主要的混淆点是你的所有代码都在一个方法(main)中,该方法又包含一个方法本地类(CalculateListener),它有 80 行长。

您的意思是要成为一个方法本地类吗?您是否有任何理由希望它成为方法本地类您是否真的忘记“关闭”您的main方法?无论如何,您甚至似乎都不使用 ,或者对所创建的内容做任何事情。CalculateListenerJFramemain

如果您要求 IDE 为您缩进代码,当您遇到此类问题时,它应该非常清楚。此外,缩短方法并尝试减少缩进会有所帮助。例如,在您的actionPerformed方法中,方法的整个主体都在一个if块内。如果你只是反转 that 的逻辑if,你可以节省一层嵌套。然后,您也可以对下一个块执行相同的操作if

public void actionPerformed (ActionEvent ae)
{
    if (ae.getSource() != displayButton)
    {
        return;
    }
    resultTextArea.setText("");
    if (!readFile("input.txt"))
    {
        return;
    }
    for (int i=0; i < lines.length; i++)
    {
        if (i % 2 == 0)
        {
            // display the array elements to text area
            resultTextArea.append(lines[i]+"\n");
        }
    }
}
于 2012-03-13T07:10:22.273 回答
0

问题似乎与花括号有关:

  1. 在关闭 main() 方法和类}之后添加 2 个结束花括号frame.add(panel);
  2. }从最后一行删除
于 2012-03-13T07:15:49.073 回答