1 2 Previous Next 15 Replies Latest reply: Nov 23, 2008 1:53 PM by 3004 RSS

    class comparing two text files

    843785
      Alright, so I'm supposed to write a class that compares two text files and detects whether or not those two files have any differences from each other. Then, it gives the number of differences based on chars...I am also supposed to make a client that tests it. I started writing it, and to be honest, I am not even sure if I'm doing it right....if I were to compare to text files in a method, how would I incorporate two text files in that? Whenever I see and write methods, it seems that I can only use one file at a time...the only other thing I can think of is making a new object-type thing, like
      TextChecker text = new TextChecker( "file1.txt", "file2.txt")
      ...I'm not sure what is the correct thing to do...any ideas??? Any help would be greatly appreciated. Thank you!

      Here is my class so far:
      /* FileChecker
         Smith, Tahnee
         */
      
      import java.io.File;
      import java.util.Scanner;
      import java.io.FileNotFoundException;
      
      public class FileChecker
      {
        private File file1;
        private File file2;
        
        public FileChecker( File newFile1, File newFile2)
        {
          file1 = newFile1;
          file2 = newFile2;
        }
        
        public void setFile( File setFile1, File setFile2)
        {
          file1 = setFile1;
          file2 = setFile2;
        }
        
        public void getFile( File getFile1, File getFile2)
        {
          file1 = getFile1;
          file2 = getFile2;
        }
        
        public int textChecker(int difference)
        {
          
        try 
        {
          Scanner file3 = new Scanner( file1);
          Scanner file4 = new Scanner( file2);
          
          while( file3.hasNext() && file4.hasNext())
          {
            String stringRead1 = file3.nextLine();
            String stringRead2 = file4.nextLine();
            
           
            
            for( int i = 0; i < stringRead1.length(); i++)
            {
              for( int j = 0; j < stringRead2.length(); i++)
              {
                if (stringRead1.charAt(i) != stringRead2.charAt(j))
                {
                  difference++;
                  
                }
              }
            }
            System.out.println( "The number of differences are " + difference);
          }
         }
        catch(FileNotFoundException fnfe)
        {
          System.out.println( "Files were not found ");
        }
        return difference; 
      }
      }
        • 1. Re: class comparing two text files
          843785
          That getter doesn't do anything, java only passes by reference.
          Normally, setters only set one value.
          • 2. Re: class comparing two text files
            843785
            Scanner ignores spaces and you ignore if one file is longer than the other.
            You also compare every character with every character the second string ignoring the position of that character.
            • 3. Re: class comparing two text files
              843785
              So basically, I don't really need to have the set and get methods at all?
              • 4. Re: class comparing two text files
                843785
                Hmmm okay so I think it would be better to see how many line differences there are??? How do I do that?? My instructor hasn't told us of how to do this...nor has taught us how to specifically make classes with files that you read....so I know its pretty awkward
                • 5. Re: class comparing two text files
                  843785
                  guitarlady3000 wrote:
                  So basically, I don't really need to have the set and get methods at all?
                  Most getters and setters are not needed, but that is just my opinion.
                  In this case, you don't call those methods, so you don't need them. The getFile() method wouldn't work if you called it anyway.
                  • 6. Re: class comparing two text files
                    3004
                    Peter__Lawrey wrote:
                    That getter doesn't do anything, java only passes by reference.
                    By value.
                    • 7. Re: class comparing two text files
                      3004
                      guitarlady3000 wrote:
                      Alright, so I'm supposed to write a class that compares two text files and detects whether or not those two files have any differences from each other. Then, it gives the number of differences based on chars..
                      First you have to be clear about how "number of differences" is defined. Will the files always be the same size? If not, what does the text that is present in one file and not the other contribute to the difference count?

                      Is it as simple as check every character, and if they don't match, add one to the difference count?
                      • 8. Re: class comparing two text files
                        843785
                        The files will always be the same size, and yes, it is as simple as check every character, difference is incremented.
                        • 9. Re: class comparing two text files
                          3004
                          guitarlady3000 wrote:
                          The files will always be the same size, and yes, it is as simple as check every character, difference is incremented.
                          Okay, so given that and the other discussion in this thread so far, what does your code look like and what specific questions do you have?
                          • 10. Re: class comparing two text files
                            843785
                            Even that can be fairly complex thing to do so try that first.
                            You can read each pair of line and use equals() to compare them.
                            This will work fine for simple cases so to improve it you can.
                            - check that one file is longer than the other.
                            - check that a line was added or removed. (If you don't every line after it will be different)
                            - compare lines ignoring white-space difference.
                            - check that a line was moved (probably not needed for your assignment)

                            This page may be useful
                            [http://en.wikipedia.org/wiki/Diff]
                            • 11. Re: class comparing two text files
                              3004
                              Peter__Lawrey wrote:
                              Even that can be fairly complex thing to do so try that first.
                              You can read each pair of line and use equals() to compare them.
                              This will work fine for simple cases so to improve it you can.
                              - check that one file is longer than the other.
                              - check that a line was added or removed. (If you don't every line after it will be different)
                              - compare lines ignoring white-space difference.
                              - check that a line was moved (probably not needed for your assignment)

                              This page may be useful
                              [http://en.wikipedia.org/wiki/Diff]
                              It sounds like all he has to do is compare same-sized files and count the number of non-matching characters. A far cry from implementing diff.
                              • 12. Re: class comparing two text files
                                843785
                                yes, all I ( a she, specifically) have to do is check to see if some characters are different from the other text file, which is going to be the same length...All I need to know if I am writing the header of the method right. Again, I need to know how I will incorporate two text files in a method...for exapmle...methods usually look like this:
                                text1.TextChecker()
                                , but that only uses one text file...I need to use two text files...so do I need to write it like a method or like a new object that will look like this
                                TextChecker text = new TextChecker( "file1.txt", "file2.txt")
                                Orrrr am I just getting the wrong idea? I may be just confusing myself too much...
                                • 13. Re: class comparing two text files
                                  3004
                                  guitarlady3000 wrote:
                                  yes, all I ( a she, specifically) have to do is check to see if some characters are different from the other text file, which is going to be the same length...All I need to know if I am writing the header of the method right. Again, I need to know how I will incorporate two text files in a method...for exapmle...methods usually look like this:
                                  text1.TextChecker()
                                  , but that only uses one text file...I need to use two text files...so do I need to write it like a method or like a new object that will look like this
                                  TextChecker text = new TextChecker( "file1.txt", "file2.txt")
                                  You can either have a method that takes two filenames and returns an int, or you can have a constructor that takes the two filenames, and then a method that does the count and returns the difference.

                                  The latter approach--providing the filenames in the constructor--only makes sense if you're going to keep that object around and do other things with those two files after counting their differences.
                                  • 14. Re: class comparing two text files
                                    843785
                                    Ok, thank you very much!!!!!!! That is all I needed to know :-)
                                    1 2 Previous Next