Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串的JSP输入数组_Java_Jsp - Fatal编程技术网

Java 字符串的JSP输入数组

Java 字符串的JSP输入数组,java,jsp,Java,Jsp,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>

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>

JSP页面
输入第一个输入

输入第二个输入
剽窃

 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>

}
import java.io.IOException;
导入java.io.PrintWriter;
导入javax.servlet.ServletException;
导入javax.servlet.annotation.WebServlet;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
@WebServlet(“/Plagarism”)
公共类Plagarism扩展了HttpServlet
{
受保护的void processRequest(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
setContentType(“text/html;charset=UTF-8”);
PrintWriter out=response.getWriter();
out.println(“”);
out.println(“”);
out.println(“”);
out.println(“Servlet瘟疫”);
out.println(“”);
out.println(“”);
字符串s1=request.getParameter(“File1”);
字符串s2=request.getParameter(“File2”);
String[]words=s1.split(“”);
字符串[]words2=s2.split(“”);
int计数器=0;
对于(inti=0;i0){
out.println(“Plag发现”);
} 
否则{
out.println(“未找到plag”);
}
计数器=0;
//out.println(“比较两个文件……”);
//println(“这两个文件的结果是…”);
/*
if(fileOne.equals(fileTwo)){
out.println(“发现剽窃。骗子!!”);
}否则{
out.println(“未发现剽窃”);
}
*/
out.println(“”);
out.println(“”;}
// 
@凌驾
受保护的void doGet(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
processRequest(请求、响应);
}
@凌驾
受保护的void doPost(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
processRequest(请求、响应);
}
@凌驾
公共字符串getServletInfo(){
返回“简短描述”;
}// 
}
这是一个程序,用于检查从浏览器输入的两个句子中是否有常用词。代码工作不正常,显示所有输入均未发现剽窃。请告知我此代码中出现了什么错误。 输入的格式正确,但仍不起作用


代码是用Java和JSP编写的。

您遇到的问题是,您的
for
循环忽略了最后一个单词,因为您只循环到
i
。这应该是
i

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"

此外,替换所有空白似乎毫无意义,除非您希望输入中返回制表符或回车符

给定的输入忽略了空格-您在空格上拆分这行是一个输入错误。当您在
上拆分时,
为什么要在
s+
上进行正则表达式替换?您是对的。我不需要。只要.equals()就可以了。好的
equalsIgnoreCase
可能很好
for (String word : words) {
    /* ... */
}