为什么此错误消息与另一个消息一起出现?

时间:2018-05-16 06:33:57

标签: java compiler-errors

System.out.println("Hello, world!);

(我知道规则但是想知道编译器是否投诉)

这会产生两个不同的错误消息,即使只有一个错误消息 基础语法错误:

第一条消息: -

Hello.java:3: unclosed string literal <br>
System.out.println("Hello, world!);

另一条消息: -

Hello.java:4: ')' expected before }

3 个答案:

答案 0 :(得分:5)

首先,你没有结束"。这是第一条错误消息告诉您的内容,但我想您对第二条消息出现的原因更感兴趣。

由于您错过了""之后的所有内容都被视为字符串:

Hello, world!);

您会看到)也被视为字符串的一部分,这就是编译器无法找到)的原因。

有趣的事实:这样做:

System.out.println("Hello World
        ); // note the new line

将使第二条消息消失但不是第一条消息。这是因为字符串文字不能跨越多行。编译器找到一个未封闭的字符串文字,因此是第一条消息,但这次可以找到),所以没有第二条消息。

答案 1 :(得分:0)

<强>简单地

System.out.println("Hello, world!);
//First error as your expectations you didn't closed string literal using ".   
//Second error because you didn't enclosed your string in " and " then compiler took ');' as part of string and hence it saying you you missed ')' before '}'.

//Replace your line with below lines

    //START
    System.out.println("Hello, world!);

    ; 
    //END
//When you replace your line with my code given where ';' represent line end then you will get only first error.

答案 2 :(得分:-1)

这是由于您缺少报价的原因。 字符串应该用Java中的双引号括起来。 例如,String name =&#34; java&#34; ,String stackoverflow =&#34;你好&#34;

相关问题