Merging two files [editted]

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2008
Posts: 65
Reputation: StarZ is an unknown quantity at this point 
Solved Threads: 1
StarZ StarZ is offline Offline
Junior Poster in Training

Merging two files [editted]

 
0
  #1
Nov 4th, 2009
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;

  1. public class Main {
  2.  
  3.  
  4. public static void main(String[] args) {
  5. FileReader file1=new FileReader("Data1.txt");
  6. Scanner scan = new Scanner(new File("Data1.txt"));
  7. ArrayList<Integer> values = new ArrayList<Integer>();
  8. collections.sort(values);
  9.  
  10. while(scan.hasNextInt()) values.add(scan.nextInt());
  11.  
  12. FileReader file2=new FileReader("Data2.txt");
  13. Scanner scan = new Scanner(new File("Data2.txt"));
  14. ArrayList<Integer> values = new ArrayList<Integer>();
  15. collections.sot(values);
  16.  
  17. while(scan.hasNextInt()) values.add(scan.nextInt());
  18.  
  19. BufferedReader br1 = new BufferedReader (file1);
  20. BufferedReader br2 = new BufferedReader (file2);
  21.  
  22. String temp1, temp2;
  23. while(br1.readLine() !=null)
  24. {
  25. temp1=br1.readLine()+temp1;
  26. }
  27. while(br2.readLine()!=null)
  28. {
  29. temp2=br2.readLine()+temp2;
  30. }
  31. String temp = temp1 + temp2;
  32. FileWriter fw=new FileWriter("data3.txt");
  33. char buffer[]=new char[temp.length];
  34. temp.getChars(0,temp.length(),buffer,0);
  35. fw.write(buffer);
  36. file1.close();
  37. file2.close();
  38. fw.close();
  39. }
  40. }

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 ><"
Last edited by StarZ; Nov 4th, 2009 at 11:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,657
Reputation: BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold BestJewSinceJC is a splendid one to behold 
Solved Threads: 206
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso
 
0
  #2
Nov 5th, 2009
http://www.daniweb.com/forums/thread235793.html

You need to mark solved threads as solved - not to do so (and not even a thank you post)? is rude.

  1. collections.sot(values);

There is no sot method. Or collections class. It's Collections.sort(values). . the case is important. And you should only call Collections.sort() after you add all the values from both files to the array. You are sorting after you add the values from one file, which does nothing useful. Then, after you sort, you should print the values out to a file.

edit: And there is no need for the extra array "temp" or anything like that. You should just create an instance of some class that allows you to write out to a file. You should then use that Object to write each element in your array out to a file using a for loop. There are a lot of examples online of writing to a file. . I suggest PrintWriter, as I think I did in the other thread I helped you in.
Last edited by BestJewSinceJC; Nov 5th, 2009 at 2:58 am.
Out.
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 186 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC