java 此行有多个标记 - 标记“)”上的语法错误, ; 预期 - 标记“(”上的语法错误,{预期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16137302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:01:20  来源:igfitidea点击:

Multiple markers at this line - Syntax error on token ")", ; expected - Syntax error on token "(", { expected

javajava-7parenthesestry-finally

提问by user2305453

I'm studying Java (sorry for my poor english, it's not my native language) and when I do a "try-finally" block in Eclipse (JavaSE-1.7) in every "try" that I put, appears this message:

我正在学习 Java(对不起,我的英语不好,它不是我的母语),当我在 Eclipse (JavaSE-1.7) 中在每次“尝试”中执行“try-finally”块时,会出现以下消息:

Multiple markers at this line - Syntax error, insert "}" to complete Block - Syntax error, insert "Finally" to complete BlockStatements

此行有多个标记 - 语法错误,插入“}”以完成 Block - 语法错误,插入“Finally”以完成 BlockStatements

Here is the full code:

这是完整的代码:

package Java;

public class Arquivo3 {

    private Path BdC = Paths.get("C:/xti/files/conta.txt");
    private Charset utf8 = StandardCharsets.UTF_8;

    public void armazenarContas(ArrayList<Conta> contas) throws IOException{                
        try(BufferedWriter writer = Files.newBufferedWriter(BdC, utf8)) {
            for (Conta conta : contas) {
                writer.write(conta.getCliente() + ";" + conta.getSaldo() + "\n");
            }
        }
    }

    public ArrayList<Conta> recuperarContas() throws IOException{
        ArrayList<Conta> contas = new ArrayList<Conta>();
        try (BufferedReader reader = Files.newBufferedReader(BdC, utf8)){
            String line = null;
            while((line = reader.readLine()) != null) {
                String[] t = line.split(";");
                Conta conta = new Conta(t[0], Double.parseDouble(t[1]));
                contas.add(conta);
            }
        }finally {
        return contas;  
        }

    }

    public static void main(String[] args) throws IOException{
/*
        ArrayList<Conta> contas = new ArrayList<Conta>();
        contas.add(new Conta("Ricardo", 12000.23));
        contas.add(new Conta("Lawrence", 11050.32));
        contas.add(new Conta("Sandra", 18000.21));
        contas.add(new Conta("Beatriz", 23200.09));
    */  
        Arquivo3 operacao = new Arquivo3();
        //operacao.armazenarContas(contas);
        ArrayList<Conta> contas2 = operacao.recuperarContas();
        for (Conta conta : contas2) {
            conta.exibeSaldo();
        }
    }

}

回答by Jayan

Using the comment from OP

使用来自 OP 的评论

@yan Odd, I use jdk7 and this works for me, but then, maybe eclipse uses jdk6 for testing for syntax errors. – gangqinlaohu 2 hours ago

@yan Odd,我使用 jdk7,这对我有用,但是,也许 eclipse 使用 jdk6 来测试语法错误。– gangqinlaohu 2 小时前

That is correct. You are using some jdk7 features but eclipse is configured to use jdk6 (or older). Here are the instructions to enable jdk7 with eclipse ( changing eclipse's java compiler to jdk7).

那是对的。您正在使用一些 jdk7 功能,但 eclipse 配置为使用 jdk6(或更旧)。以下是使用 eclipse 启用 jdk7 的说明( 将 eclipse 的 java 编译器更改为 jdk7)。

Specifically, compliance setting.

具体来说,合规设置。

回答by 0x6C38

As Eclipse says, in your armazenarContas()method you do not finish your try{}statement with a finally(or anything else for that matter). You need to add either a catch{}or a finally{}at the end of your try{}.

正如 Eclipse 所说,在您的armazenarContas()方法中,您不会try{}以 a finally(或其他任何内容)结束您的语句。您需要添加或者是catch{}finally{}在你结束try{}

Your syntax should be something like:

你的语法应该是这样的:

try {
//Do stuff
} catch (ExceptionType name) {
//If an exception occours, you can handle it here.
}

or:

或者:

try {
//Do stuff
} finally {
//Everything in this block of code will be excecuted
}

or you can even combine them all:

或者你甚至可以将它们全部结合起来

try{
//Do stuff
} catch (ExceptionType name) {
//If you get an exception
} finally {
//Always excecuted
}