的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>
Plagiarism.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。
答案 0 :(得分:1)
您遇到的问题是,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()
时可能会更好地补偿这一点。