Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please fix the code it loops util finds all the differences in two files. import

ID: 3788424 • Letter: P

Question

Please fix the code it loops util finds all the differences in two files.

import java.io.*;

import java.util.Scanner;

public class {

   public static void differences(Scanner file1, Scanner file2)

           throws IOException{

      

       String firstLines = file1.nextLine();

       String secondLines = file2.nextLine();

      

       while(firstLines != null || secondLines != null){

           int lineNum = 1;

           int comparison = firstLines.compareTo(secondLines);

           System.out.println("Differences found:");

               if(comparison != 0){

                   System.out.println("Line "+lineNum);

                   System.out.println("<"+firstLines);

                   System.out.println(">"+secondLines);

                   System.out.println(" ");

               }

               break;

       }

              

   }

          

public static void main (String[]args)

           throws FileNotFoundException, IOException{

  

        Scanner console = new Scanner(System.in);

        System.out.println("Please enter the name of the first file: ");

        File firstFile = new File(console.nextLine());

        Scanner userFile1 = new Scanner(firstFile);

        System.out.println("Please enter the name of the second file: ");

        File secondFile = new File(console.nextLine());

        Scanner userFile2 = new Scanner(secondFile);

        console.close();

       

        differences(userFile1, userFile2);

   }

}

Explanation / Answer

import java.io.*;
import java.util.Scanner;
public class A {
public static void differences(Scanner file1, Scanner file2)
throws IOException{
  
String firstLines = file1.nextLine();
String secondLines = file2.nextLine();
  
while(firstLines != null || secondLines != null){
int lineNum = 1;
int comparison = firstLines.compareTo(secondLines);
System.out.println("Differences found:");
if(comparison != 0){
System.out.println("Line "+lineNum);
System.out.println("<"+firstLines);
System.out.println(">"+secondLines);
System.out.println(" ");
}
break;
}
  
}
  
public static void main (String[]args)
throws FileNotFoundException, IOException{

Scanner userFile1=null,userFile2=null;
Scanner console = new Scanner(System.in);
System.out.println("Please enter the name of the first file: ");
System.out.flush();
try{
String filename =console.nextLine();
File firstFile = new File(filename);
userFile1 = new Scanner(firstFile);}
catch(Exception e){e.printStackTrace();}
  
  
  

  
System.out.println("Please enter the name of the second file: ");
try{
File secondFile = new File(console.nextLine());
userFile2 = new Scanner(secondFile);
}
catch(Exception e1){
e1.printStackTrace();}
finally{
console.close();}

differences(userFile1, userFile2);
}
}