我在尝试编译时不断收到此消息: 解析时到达文件结尾 谁知道为什么?我唯一想到的是它与我的花括号有关。我试图移动大括号,添加它们并删除它们,但我无法解决这个问题。错误发生在最后一行代码中。
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");
}
}
}
}
}
}
}
答案 0 :(得分:3)
是的,你没有足够的关闭括号。
一个主要的混淆点是你的所有代码都在一个方法(main
)中,而这个方法又包含一个80行的方法本地类(CalculateListener
)。
你意味着是一个方法本地类吗?是否有任何理由让你想要它成为一个方法本地类你真的忘了“关闭”你的main
方法吗?您似乎甚至无法使用 CalculateListener
,或对JFrame
创建的main
执行任何操作。
如果您要求IDE为您缩进代码,那么当您遇到类似问题时,应该非常清楚。此外,缩短您的方法并尝试减少缩进可能会有所帮助。例如,在您的actionPerformed
方法中,方法的整个主体位于单个if
块中。如果您只是反转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");
}
}
}
答案 1 :(得分:0)
问题似乎是花括号:
}
之后添加2个结束花括号frame.add(panel);
以关闭main()方法和类}