Java:请插入“}”以完成classbody

时间:2011-09-13 16:28:40

标签: java class syntax syntax-error

我写了一个小测试程序,但我在结束标签中遇到语法错误......

这是代码

public class Test 
{
    AudioFile file = null;
    String vbb = "";
    File f;

    public Test()
    {
        openFile();
    }   

    public File openFile()
    {
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int result = fc.showOpenDialog(fc);
        if(result == JFileChooser.CANCEL_OPTION)
        {
            return null;
        } else {
            f = fc.getCurrentDirectory();
            return f;
        }
    }

    f = new File(openFile());
    File[] files = f.listFiles();

    for(File fi : files)
    {
        try {
            file = (AudioFile) AudioFileIO.read(new File(fi.getAbsolutePath()));
            MP3AudioHeader ah = (MP3AudioHeader) file.getAudioHeader();
            String time = ah.getTrackLengthAsString();
            String rate = ah.getBitRate();
            boolean vb = ah.isVariableBitRate();
            if(vb == false)
            {
                vbb = "Nee";
            } else {
                vbb = "Ja";
            }
            Tag tag = file.getTag();
            String artist = tag.getFirst(FieldKey.ARTIST);
            String title = tag.getFirst(FieldKey.TITLE);
            String album = tag.getFirst(FieldKey.ALBUM);
            String genre = tag.getFirst(FieldKey.GENRE);
            String temo = tag.getFirst(FieldKey.BPM);
            String path = fi.getAbsolutePath();

            System.out.println("Duur: " + time + "\nVariabele bitrate: " + vbb + "\nArtiest: " + artist +"\nTitel: " + title
                    + "\nAlbum: " + album + "\nGenre: " + genre + "\nBPM: " + temo + "\nBitrate: " + rate + " kbps\nPad: " + path);

        } catch (Exception e)
        {
            System.err.print("FOUT");
        }
    }
}

编译器在LATEST结束奖励时给出错误: “请插入}以完成课堂主体” 而且在“openFile()”方法的最后一个荣誉......

有什么建议吗?

5 个答案:

答案 0 :(得分:5)

    f = new File(openFile());
    File[] files = f.listFiles();

    for(File fi : files)
    {
        //...
    }

这整个逻辑块不在方法中。它需要在方法或构造函数中。

答案 1 :(得分:4)

你在哪里

f = new File ...
...
catch ( .. )
{
  ....
}

你想把它包装在

public static void main (String args[]) {
....
}

您不能在类定义中拥有代码块。在类的最顶层,这些变量声明是具有默认可见性的类成员的声明。

答案 2 :(得分:3)

所有以行

开头的代码
f = new File(openFile());

不在任何方法之内。这不是合法的Java:语句必须包含在块或方法体中。

答案 3 :(得分:0)

以下所有内容

public File openFile()
{
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int result = fc.showOpenDialog(fc);
    if(result == JFileChooser.CANCEL_OPTION)
    {
        return null;
    } else {
        f = fc.getCurrentDirectory();
        return f;
    }
}

不包含在方法体内,而是潜伏在类体中。移除上方的外部支撑。

答案 4 :(得分:0)

您的代码不在方法中。它需要在方法或静态块中。猜测你的意图你可以把它放在构造函数中:

public class Test 
{
    AudioFile file = null;
    String vbb = "";
    File f;

    public Test()
    {
        openFile();

        f = new File(openFile());
        File[] files = f.listFiles();

        for(File fi : files)
        {
            try {
                file = (AudioFile) AudioFileIO.read(new File(fi.getAbsolutePath()));
                MP3AudioHeader ah = (MP3AudioHeader) file.getAudioHeader();
                String time = ah.getTrackLengthAsString();
                String rate = ah.getBitRate();
                boolean vb = ah.isVariableBitRate();
                if(vb == false)
                {
                    vbb = "Nee";
                } else {
                    vbb = "Ja";
                }
                Tag tag = file.getTag();
                String artist = tag.getFirst(FieldKey.ARTIST);
                String title = tag.getFirst(FieldKey.TITLE);
                String album = tag.getFirst(FieldKey.ALBUM);
                String genre = tag.getFirst(FieldKey.GENRE);
                String temo = tag.getFirst(FieldKey.BPM);
                String path = fi.getAbsolutePath();

                System.out.println("Duur: " + time + "\nVariabele bitrate: " + vbb + "\nArtiest: " + artist +"\nTitel: " + title
                        + "\nAlbum: " + album + "\nGenre: " + genre + "\nBPM: " + temo + "\nBitrate: " + rate + " kbps\nPad: " + path);

            } catch (Exception e)
            {
                System.err.print("FOUT");
            }
        }


    }   

    public File openFile()
    {
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int result = fc.showOpenDialog(fc);
        if(result == JFileChooser.CANCEL_OPTION)
        {
            return null;
        } else {
            f = fc.getCurrentDirectory();
            return f;
        }
    }

}