Repository navigation
Expand file tree
/
Copy pathLab8.java
More file actions
104 lines (98 loc) · 3.47 KB
/
Copy pathLab8.java
File metadata and controls
104 lines (98 loc) · 3.47 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import java.io.*;
import java.util.Scanner;
public class Lab8 {
int k = 8;
public static void main(String[] args) throws IOException {
Lab8 m = new Lab8();
m.create();
m.mergeFiles();
m.display();
}
public void create() throws FileNotFoundException {
Scanner scan = new Scanner(System.in);
for (int i = 1; i <= k; i++) {
System.out.println("Enter the of names in list " + i + ". Enter # to terminate list");
PrintWriter pw = new PrintWriter("list" + i + ".txt");
String[] temp = new String[50];
String str;
int j = 0;
while (!((str = scan.nextLine()).equals("#")))
temp[j++] = str;
sort(temp, j);
for (int k = 0; k < j; k++)
pw.println(temp[k]);
pw.flush();
pw.close();
}
scan.close();
}
public void sort(String[] s, int count) {
String temp;
for (int i = 0; i < count; i++)
for (int j = i + 1; j < count; j++)
if (s[i].compareTo(s[j]) > 0) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
public void mergeFiles() throws IOException {
int n = k;
while (n > 1) {
int count = 1;
for (int i = 1; i <= n; i += 2) {
File f1 = new File("list" + i + ".txt");
File f2 = new File("list" + (i + 1) + ".txt");
File f3 = new File("list" + i + (i + 1) + ".txt");
BufferedReader br1 = new BufferedReader(new FileReader(f1));
BufferedReader br2 = new BufferedReader(new FileReader(f2));
PrintWriter pw = new PrintWriter(f3);
String name1 = br1.readLine();
String name2 = br2.readLine();
while (name1 != null && name2 != null) {
if (name1.equals(name2)) {
pw.println(name1);
pw.println(name2);
name1 = br1.readLine();
name2 = br2.readLine();
} else if (name1.compareTo(name2) < 0) {
pw.println(name1);
name1 = br1.readLine();
} else {
pw.println(name2);
name2 = br2.readLine();
}
}
if (name1 == null) {
while (name2 != null) {
pw.println(name2);
name2 = br2.readLine();
}
}
if (name2 == null) {
while (name1 != null) {
pw.println(name1);
name1 = br1.readLine();
}
}
pw.close();
br1.close();
br2.close();
f1.delete();
f2.delete();
f3.renameTo(new File("list" + count + ".txt"));
count++;
}
n /= 2;
}
}
public void display() throws IOException {
BufferedReader b = new BufferedReader(new InputStreamReader(new
FileInputStream("list1.txt")));
String l;
System.out.println("\nThe merged list is:");
while ((l = b.readLine()) != null)
System.out.println(l);
b.close();
}
}