问题
I keep getting this message when I try to compile: Reached End of file while parsing Anyone know why? The only thing that I have figured out is that it has something to do with my curly braces. I have tried to move braces around, add them, and delete them but I can not figure this out. The error occurs in the last line of code.
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");
}
}
}
}
}
}
}
回答1:
Yes, you don't have enough closing braces.
One major point of confusion is that all of your code is in a single method (main
) which in turn contains a method-local class (CalculateListener
) which is 80 lines long.
Did you mean that to be a method-local class? Is there any reason why you want it to be a method-local class Did you just actually forget to "close" your main
method? You don't even seem to use CalculateListener
anyway, or do anything with the JFrame
that main
creates.
If you ask your IDE to indent your code for you, it should make it very clear when you have problems like this. Additionally, making your methods shorter and trying to reduce indentation can help. For example, in your actionPerformed
method, the whole body of the method is within a single if
block. If you just invert the logic of that if
, you can save a level of nesting. You can then do the same with the next if
block, too:
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");
}
}
}
回答2:
The problem seems to be with curly braces:
- Add 2 ending curly braces
}
afterframe.add(panel);
to close your main() method and class - Remove
}
from last line
来源:https://stackoverflow.com/questions/9679523/reached-end-of-file-while-parsing