java 字符串文字没有被双引号正确关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35668136/
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-11-03 00:26:14  来源:igfitidea点击:

String literal is not properly closed by a double-Quote

javacompiler-errors

提问by Sathish Kumar

I'm getting an error in the line no 16(starting with '"<!DOCTYPE...') on my java file.

我在"<!DOCTYPEjava 文件的第 16 行(以“ ...”开头)中出现错误。

out.println ( 
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"

\"http://www.w3.org/TR/html4/loose.dtd\">\n" + 
"<html> \n" + 
"<head> \n" + 
"<meta http-equiv=\"Content-Type\" content=\"text/html; 
charset=ISO-8859-1\"> \n" + 
"<title> My first jsp </title> \n" + 
"</head> \n" + 
"<body> \n" + 
"<font size=\"12px\" color=\"" + color + "\">" + 
"Hello World" + 
"</font> \n" + 
"</body> \n" + 
"</html>" 
);

Error Messge:

错误信息:

String literal is not properly closed by a doubl-Quote

字符串文字没有被双引号正确关闭

Dont know whats wrong. Please help me.

不知道怎么回事。请帮我。

回答by

String literals are not allowed to span more than one line in Java.

在 Java 中,字符串文字不能跨越多于一行。

Here you are trying to create a string literal that spans more than one line:

在这里,您尝试创建一个跨越多行的字符串文字:

"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
\"http://www.w3.org/TR/html4/loose.dtd\">\n" 

Instead, split it up in multiple concatenated strings like this:

相反,将其拆分为多个串联字符串,如下所示:

"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"" +
"\"http://www.w3.org/TR/html4/loose.dtd\">\n"

Or place the entire string on one line like this:

或者像这样将整个字符串放在一行上:

"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"

The same issue occurs again at the <meta>tag.

同样的问题再次出现在<meta>标签上。