1

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>

}

This is the program to check if there are common words in the 2 sentences taken as input from browser.The code is not working properly and is showing not found plagiarism for all inputs.Please inform me what has gone wrong in this code. The input is in right format still it is not working.

The code is in Java and JSP.

5
  • The input given is ignoring the spaces - well you are splitting on spaces Commented Apr 17, 2018 at 8:24
  • That line was a typo. Commented Apr 17, 2018 at 8:27
  • 1
    As you are splitting on " " why are you doing a regex replaceAll on s+ ? Commented Apr 17, 2018 at 8:31
  • You are right. i dont need that. just .equals() will work. Commented Apr 17, 2018 at 8:36
  • 1
    well equalsIgnoreCase is maybe good Commented Apr 17, 2018 at 8:37

1 Answer 1

1

The problem you have is that your for loops leave the last word out of consideration because you only loop until i < words.length - 1. That should be 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"

Or, you could use the newer foreach construct to avoid these kinds of issues:

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

Also, the replacing of all the white-space seems pretty meaningless, unless you're expecting tabs or carriage returns in your input. In that case, it would probably be better to compensate for that in your call to split().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.