Reputation: 389
I have 2 text files that contain numbers (integers) that are sorted in ascending order. Now I want to create a new text file containing all the numbers from both files. In other words, the values in the two files must be merged into a new file. The numbers in the new file must also be in ascending order.
For example:
file1: 1 3 5 7 9
file2: 2 4 6 8 10
The new file must contain: 1 2 3 4 5 6 7 8 9 10
In the method I've created, the problem is that it starts scanning the first numbers in both files file1: 1 file2: 2. It finds that the number in file1 is the least the scanner goes on to the next number in both files. So the next thing that is being scanned is file1: 3 file2: 4. then it is 3 which is the least. However, the number 2 from the first scan and the number 4 from the second scan have been skipped. Here is my code so far:
public static void mergeAllNumbers(String fileName1, String fileName2, String fileNameNew)
throws FileNotFoundException {
Scanner scanFile1 = new Scanner(new File(fileName1));
Scanner scanFile2 = new Scanner(new File(fileName2));
PrintWriter writeToFile = new PrintWriter(fileNameNew);
// merge as long as there is something in both files
while (scanFile1.hasNextInt() && scanFile2.hasNextInt()) {
int value1 = scanFile1.nextInt();
int value2 = scanFile2.nextInt();
if (value1 <= value2) {
// The number in fileName1 is less
writeToFile.write(value1 + "");
writeToFile.write(System.getProperty("line.separator"));
} else { // The number in fileName2 is less
writeToFile.write(value2 + "");
writeToFile.write(System.getProperty("line.separator"));
}
}
// empty the file which is not empty
while (scanFile1.hasNextInt()) {
int value1 = scanFile1.nextInt();
writeToFile.write(value1 + "");
writeToFile.write(System.getProperty("line.separator"));
}
while (scanFile2.hasNextInt()) {
int value2 = scanFile2.nextInt();
writeToFile.write(value2 + "");
writeToFile.write(System.getProperty("line.separator"));
}
scanFile1.close();
scanFile2.close();
writeToFile.close();
}
Upvotes: 1
Views: 405
Reputation: 5589
The things I see you are missing:
1) After the fist time when you do read both files, you shold only read the from the file you are writting. (if you write the value of file1, then read the next value from file 1).
2) Consider both values are equal, in that case you do write both values, and read the next value from both files. This is assuming the merge implies have all the record merged even if they are duplicated.
The while loop should look like this:
int value1 = -1; // -1 just to give it an initial value
int value2 = -1;
boolean eof1 = !scanFile1.hasNextInt();
booelan eof2 = !scanFile2.hasNextInt();
if(!eof1){
value1 = scanFile1.nextInt();
}
if(!eof2){
value2 = scanFile2.nextInt();
}
while (!eof1 && !eof2) {
if (value1 < value2) {
// The number in fileName1 is less
writeToFile.write(value1 + "");
writeToFile.write(System.getProperty("line.separator"));
eof1 = !scanFile1.hasNextInt();
if(!eof1){
value1 = scanFile1.nextInt();
}
} else if(value1 > value2){ // The number in fileName2 is less
writeToFile.write(value2 + "");
writeToFile.write(System.getProperty("line.separator"));
eof2 = !scanFile2.hasNextInt();
if(!eof2){
value2 = scanFile2.nextInt();
}
}else{ // they are equal
writeToFile.write(value1 + "");
writeToFile.write(System.getProperty("line.separator"));
eof1 = !scanFile1.hasNextInt();
if(!eof1){
value1 = scanFile1.nextInt();
}
eof2 = !scanFile2.hasNextInt();
if(!eof2){
value2 = scanFile2.nextInt();
}
}
}
Upvotes: 1
Reputation: 71
Since you're invoking .nextInt() on both files, you will always lose the number that you don't use. You could read both into two separate arrays, and then perform a merge sort on them. https://www.geeksforgeeks.org/merge-sort/
Upvotes: 0