-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyScanFile
37 lines (31 loc) · 1.28 KB
/
MyScanFile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.*;
import java.io.*;
// MyScanFile reads to different files and prints the line number and the line at which the two files differ.
public class MyScanFile {
// Main method reads two given file names and passes the scanners
// to method differences().
public static void main (String[] args) throws FileNotFoundException {
System.out.println("Enter name of File 1: ");
Scanner console = new Scanner(System.in);
String fileName1 = console.next();
System.out.println("Enter name of File 2: ");
String fileName2 = console.next();
Scanner input1 = new Scanner(new File(fileName1));
Scanner input2 = new Scanner(new File(fileName2));
System.out.println("Differences found:");
differences(input1, input2);
}
// The method differences() compares each line of the files to eachother.
// If a difference is found, the line numbers and lines are printed.
public static void differences(Scanner input1, Scanner input2) {
int lineCounter = 1; //line number
while (input1.hasNextLine() && input2.hasNextLine()) {
String sameLine1 = input1.nextLine();
String sameLine2 = input2.nextLine();
if (!sameLine1.equals(sameLine2)) {
System.out.println("Line" + lineCounter + ":\n< " + sameLine1 + "\n> " + sameLine2); //prints line when different
}
lineCounter++;
}
}
}