I'm supposed to merge a ordered data of two files into a third file, keeping the data in order. I'm suppose to create a MergeFiles application that merges the integers ordered from low to high in two files into a third file, keeping the order from low to high. Then should merge the two files by taking one element at a time from each, and the third file should contain the numbers from both file from lowest to highest. so, i saved the numbers in wordpad as data1.txt, and data2.txt.
Data1: 11 25 36 45 56 78 90
Data2: 1 3 5 7 54 32 78 99
Then the third data should output:
data3: 1 3 5 7 11 25 32 36 45 54 56 ..... so on (from low to high)
So far I have this:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
public class Main {
public static void main(String[] args) {
FileReader file1=new FileReader("Data1.txt");
Scanner scan = new Scanner(new File("Data1.txt"));
ArrayList<Integer> values = new ArrayList<Integer>();
collections.sort(values);
while(scan.hasNextInt()) values.add(scan.nextInt());
FileReader file2=new FileReader("Data2.txt");
Scanner scan = new Scanner(new File("Data2.txt"));
ArrayList<Integer> values = new ArrayList<Integer>();
collections.sot(values);
while(scan.hasNextInt()) values.add(scan.nextInt());
BufferedReader br1 = new BufferedReader (file1);
BufferedReader br2 = new BufferedReader (file2);
String temp1, temp2;
while(br1.readLine() !=null)
{
temp1=br1.readLine()+temp1;
}
while(br2.readLine()!=null)
{
temp2=br2.readLine()+temp2;
}
String temp = temp1 + temp2;
FileWriter fw=new FileWriter("data3.txt");
char buffer[]=new char[temp.length];
temp.getChars(0,temp.length(),buffer,0);
fw.write(buffer);
file1.close();
file2.close();
fw.close();
}
}
I don't know what codes I'm missing... and when i compile there are 13 errors.... I would post the errors here but it's to long.
So if u like just copy and paste this to ur java and test it.
ugh whats wrong with my program ><"