Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: I need to fix this algorithm please. Do you have any suggestion to make this algorithm working fine.thanks

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need to fix this algorithm please. Do you have any suggestion to make this algorithm working fine.thanks

    public static void file(String f)throws IOException{
            BufferedReader br3 = new BufferedReader(new FileReader(f));
            int docCount=1;
            int lineSimilar=0;
            int lineNotSimilar=0;
            try {
     
                query = "SELECT Assignment_ID,Assignment_Path FROM assignments";
                ResultSet rs = st.executeQuery(query) ;
     
                int y = 0;
                String x;
     
                String lineA = "";
                String lineB = "";
     
                int similarity = 0;
                int lineSimilar1 = 0;
                int lineSimilar2 = 0;
                String line1 ="";
                String line2 ="";
                line1 ="";
                line2 ="";
                y = 0;
                int doc=0;
     
     
                while ( rs.next() ) {
                    BufferedReader br4 = new BufferedReader(new FileReader(rs.getString("assignment_path")));
                    br3 = new BufferedReader(new FileReader(f));
                    doc = 0;
                    y = 0;
                    while((line2 = br3.readLine()) != null ){ 
                                            //problem here
                        while((line1 = br4.readLine()) != null){ 
                            if(line2.equals(line1)){           
                                y++;
     
                                //System.out.println(""+line1);
                                doc++;
     
                                lineSimilar++;
                                lineSimilar1++;
                            }else{
                                //System.out.println("line not similar");
                                lineNotSimilar++;
                            }
                        } 
                    }
                    //System.out.println("line similar"+""+lineSimilar);
     
                    if (lineSimilar1 > (doc/2)/2){
                         /*JOptionPane.showMessageDialog(null,"The result against the document number"+" "+docCount+" "+"is plagiarised");
                        JOptionPane.showMessageDialog(null,"The lines similar are:"+"  "+lineSimilar);
                        JOptionPane.showMessageDialog(null,"The lines different are:"+"  "+lineNotSimilar);*/
                        String message="";
                        for(int i=0; i<1; i++){
                            message += "The result against the document number"+" "+docCount+" "+"is plagiarised";
                            message += "\n";
                            message += "The line similar are:"+"  "+lineSimilar;
                            message += "\n";
                            message += "The line different are:"+"  "+lineNotSimilar;
                            message += "\n";
     
                        }
                        JOptionPane.showMessageDialog(null, message);
                        similarity++;
                        //i++;
                    }else{
                         String message="";
                        for(int i=0; i<1; i++){
                            message += "The result against the document number"+" "+docCount+" "+"is good";
                            message += "\n";
                            message += "The line similar are:"+"  "+lineSimilar;
                            message += "\n";
                            message += "The line different are:"+"  "+lineNotSimilar;
                            message += "\n";
     
                        }
                    }
                    docCount++;
                    }
            } catch (Exception e) {
                e.printStackTrace();
     
            }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I need to fix this algorithm please. Do you have any suggestion to make this algorithm working fine.thanks

    Please wrap your code with the code tags to preserve formatting (see Announcements - What's Wrong With My Code?)

    Do you have a question?

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need to fix this algorithm please. Do you have any suggestion to make this algorithm working fine.thanks

    sorry I'm new here!!thanks

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need to fix this algorithm please. Do you have any suggestion to make this algorithm working fine.thanks

    You are trying to compare one file with all the assignment files whose locations are stored in database. First thing to me is that there will be memory leak due to not closed buffered reader. Please correct this first. I will look deeper to your algorithm. thanks.

    --- Update ---

    You'd better split your code into different functional levels. for instance:
    public void compareFiles(String file2){
    int lineSimilar2 = 0;
    try{
    origBR = new BufferedReader(new FileReader(file));
    int doc = 0;
    int y = 0;
    String line = "";
    String line2 = "";
    while((line = origBR.readLine()) != null){
    BufferedReader br2 = new BufferedReader(new FileReader(file2));
    while((line2 = br2.readLine()) != null){
    if(line.equals(line2)){
    y++;
    doc++;
    lineSimilar++;
    lineSimilar2++;
    }
    else
    lineNotSimilar++;
    }
    }

    System.out.println(lineSimilar + " lines are similar");
    System.out.println(lineNotSimilar + " lines are not similar");
    }catch(Exception e){
    e.printStackTrace();
    }

    }

    public void compare(){
    //read in files from database
    //loop each file from resultset and compare with the original file
    compareFiles(eachFile);

    Alternatively, because you are only reading files and compare them, you may use multiple threads to compare multiple files at the same time

Similar Threads

  1. Brute force Sudoku algorithm is not working
    By cbeberge in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 19th, 2012, 03:31 PM
  2. Replies: 3
    Last Post: October 4th, 2011, 09:03 PM
  3. all i need is algorithm
    By coder.freak in forum Paid Java Projects
    Replies: 3
    Last Post: April 6th, 2011, 11:11 AM
  4. [SOLVED] Algorithm Help
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2010, 04:12 PM
  5. algorithm
    By AmmrO in forum Algorithms & Recursion
    Replies: 13
    Last Post: September 24th, 2009, 09:18 PM