我想传递 html 代码作为输入。我使用下面的代码来传递输入,但它只读取第一个 html 标签。字符串的其余部分将被忽略。我将对代码执行更多操作,而不仅仅是打印输入。
示例输入
<HTML>
<HEAD>
<TITLE>Your Title Here</TITLE>
</HEAD>
<BODY BGCOLOR="FFFFFF">
<CENTER><IMG SRC="clouds.jpg" ALIGN="BOTTOM"> </CENTER>
<HR>
<a href="http://somegreatsite.com">Link Name</a>
is a link to another nifty site
<H1>This is a Header</H1>
<H2>This is a Medium Header</H2>
Send me mail at <a href="mailto:support@yourcompany.com">
support@yourcompany.com</a>.
<P> This is a new paragraph!
<P> <B>This is a new paragraph!</B>
<a href="http://somegreatsite.com">Link Name</a>
<BR> <B><I>This is a new sentence without a paragraph break, in bold italics.</I></B>
<HR>
</BODY>
</HTML>
我的代码
public class testsolution {
public static void main(String args[])
{
String url_input = null;
Scanner s = new Scanner(System.in);
while ( s.hasNextLine()){
url_input = s.nextLine();
}
System.out.println(url_input);
}
}
我也尝试过下面的代码,但它也不起作用
String url_input = null;
Scanner s = new Scanner(System.in);
if ( s.hasNextLine()){
url_input = s.nextLine();
}
System.out.println(url_input);
我刚刚得到<HTML>
作为输出。我想传递整个代码。
最佳答案
您的问题是您只阅读了第一行。您需要有一个循环来继续阅读,直到到达输入末尾:
String url_input = null;
Scanner s = new Scanner(System.in);
while ( s.hasNextLine()){
url_input = s.nextLine();
System.out.println(url_input);
}
请注意我如何用 while
循环替换您的 if
语句。另一个变化是您的 System.out.println 应该与读取一起进行:如果您读取了一行,则将其打印出来。
关于java - 传递 HTML 代码作为输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43667361/