gpt4 book ai didi

java - JSP 输入字符串数组

转载 作者:行者123 更新时间:2023-11-30 06:12:46 25 4
gpt4 key购买 nike

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="Plagarism">

Enter the first input <textarea name="File1" rows="5" cols="10">
</textarea><br>
Enter the second input<textarea name="File2" rows="5" cols="10">
</textarea><br>
<input type="submit" value="Check Plagarism" name="btn"/>
</form>
</body>
</html>

抄袭.java

 import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Plagarism")

public class Plagarism extends HttpServlet
{

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Plagarism</title>");
out.println("</head>");
out.println("<body>");

String s1 = request.getParameter("File1");
String s2 = request.getParameter("File2");


String [] words = s1.split(" ");
String [] words2 = s2.split(" ");

int counter=0;

for (int i = 0; i < words.length-1; i++) {

for (int j = 0; j < words2.length-1; j++) {

if(words[i].equals(words2[j])) {
{
counter++;
System.out.println(words[i]);
}


}

}





/*
if(s1.replaceAll("\\s+","").equalsIgnoreCase(s2.replaceAll("\\s+","")))
out.println("Plagiarism found");
else
out.println("Plagiarism not found");
*/

if(counter>0) {
out.println("Plag found");
}
else {
out.println("plag not found");
}

counter=0;

// out.println("Comparing the 2 files......");
// out.println("The result of the 2 files is ....");

/*
if (fileOne.equals(fileTwo)) {
out.println("Plagiarism detected. Cheaters!!!!");
} else {
out.println("No plagiarism detected");
}
*/



out.println("</body>");
out.println("</html>"); }

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

}

此程序用于检查从浏览器输入的 2 个句子中是否存在常见单词。该代码无法正常工作,并且显示所有输入均未找到抄袭。请告诉我此代码中出了什么问题。输入的格式正确,但仍然不起作用。

代码采用 Java 和 JSP 编写。

最佳答案

您遇到的问题是您的 for循环不考虑最后一个单词,因为您只循环直到 i < words.length - 1 。那应该是i < words.length :

String[] words = "test me".split(" ");
String[] words2 = "you test".split(" ");

int counter = 0;

for (int i = 0; i < words.length; i++) {
for (int j = 0; j < words2.length; j++) {
if (words[i].equalsIgnoreCase(words2[j])) {
counter++;
System.out.println(words[i]); // prints "test"
}
}
}

System.out.println(counter); // prints "1"

或者,您可以使用较新的 foreach构造以避免此类问题:

for (String word : words) {
/* ... */
}

此外,替换所有空格似乎毫无意义,除非您希望输入中出现制表符或回车符。在这种情况下,最好在调用 split() 时对此进行补偿。 .

关于java - JSP 输入字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49873137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com