8 Replies - 7177 Views - Last Post: 05 November 2009 - 10:25 PM Rate Topic: -----

#1 starwok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 20-October 09

Merging two files [edited]

Posted 04 November 2009 - 08:52 PM

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 ><"

Is This A Good Question/Topic? 0
  • +

Replies To: Merging two files [edited]

#2 scrat  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 18
  • Joined: 30-June 09

Re: Merging two files [edited]

Posted 04 November 2009 - 09:27 PM

Hi starwok,

Here is a version that compiles:

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;

public class Main {

	public static void main(String[] args) {
		try {
			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");
			scan = new Scanner(new File("Data2.txt"));
			values = new ArrayList<Integer>();
			Collections.sort(values);

			while(scan.hasNextInt()) values.add(scan.nextInt());

			BufferedReader br1 = new BufferedReader (file1);
			BufferedReader br2 = new BufferedReader(file2);

			String temp1 = "";
			String 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();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}




Most of the errors are because Java forces you to handle some exception conditions, in this case IO exceptions, like if it cannot read a file.

Try to build up your programs gradually, and fix any compile errors at each stage, instead of trying to debug a completed program in one go. Life will be a lot easier :-)

Hope this helps,
scrat
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Merging two files [edited]

Posted 04 November 2009 - 09:47 PM

No need to sort before you read as you do
If you are going to load both file fully in memory (in ArrayList) no needs to have 2 ArrayList

Just read one file put the values in the arrayList
read the second file, put the values in the same arrayList

sort that arrayList and output the results

And if you are using Scanners no need to have BufferedReader

This post has been edited by pbl: 04 November 2009 - 10:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Merging two files [edited]

Posted 04 November 2009 - 10:16 PM

Something like

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;

public class Main {

	public static void main(String[] args) {
		ArrayList<Integer> values = new ArrayList<Integer>();
		try {
			Scanner scan = new Scanner(new File("Data1.txt"));
			while(scan.hasNextInt()) values.add(scan.nextInt());
			scan = new Scanner(new File("Data2.txt"));
			while(scan.hasNextInt()) values.add(scan.nextInt());
			Collections.sort(values);

			FileWriter fw=new FileWriter("data3.txt");
			for(int i = 0; i < values.size(); i++) {
				Integer x = values.get(i);
				String line = x.toString() + "\n";
				fw.write(line);
			}
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}



Not tested
Was This Post Helpful? 0
  • +
  • -

#5 starwok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 20-October 09

Re: Merging two files [edited]

Posted 05 November 2009 - 08:37 PM

This program is suppose to 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.

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)



The program compiles but when I run it, it gives me this error..... how do i fix this, so that it outputs.

> java MergeFiles
Static Error: No static method in MergeFiles with name 'main' accepts arguments (String[])
>


import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Collections;


public class MergeFiles {
	public static void MergeFiles (String[] args) {
		try {

			FileReader file1=new FileReader("File1.txt");	
			Scanner scan = new Scanner(new File("File1.txt"));
			ArrayList<Integer> values = new ArrayList<Integer>(); 
			Collections.sort(values);	   //sorting the values 
			while(scan.hasNextInt()) values.add(scan.nextInt());
		   
			FileReader file2=new FileReader("File2.txt");
			scan = new Scanner(new File("File2.txt")); 
			values = new ArrayList<Integer>();
			Collections.sort(values);		//sorting the values. 
			while(scan.hasNextInt()) values.add(scan.nextInt());
			
			BufferedReader br1 = new BufferedReader (file1);
			BufferedReader br2 = new BufferedReader(file2);
		   
			String temp1 = "";
			String temp2 = "";
			
			while(br1.readLine() !=null)
			{
			temp1=br1.readLine()+temp1;
			}
			while(br2.readLine()!=null)
			{
			temp2=br2.readLine()+temp2;
			}
			String temp = temp1 + temp2; 
		   
		   
			// Merging the numbers from low to high into a third file. 
			FileWriter fw=new FileWriter("File3.txt");   
			char buffer[]=new char[temp.length()];
			temp.getChars(0,temp.length(),buffer,0);
			fw.write(buffer);
			file1.close();
			file2.close();
			fw.close();
			
		
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
} 


Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Merging two files [edited]

Posted 05 November 2009 - 08:41 PM

hi
this line is the problem.
public static void MergeFiles (String[] args) 


to actually run your program, you should have a main method.
so change this line to:
public static void main(String[] args){
//same code
}


Was This Post Helpful? 0
  • +
  • -

#7 starwok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 20-October 09

Re: Merging two files [edited]

Posted 05 November 2009 - 09:58 PM

View Postjapanir, on 5 Nov, 2009 - 07:41 PM, said:

hi
this line is the problem.
public static void MergeFiles (String[] args) 


to actually run your program, you should have a main method.
so change this line to:
public static void main(String[] args){
//same code
}




yea, i've tried that also.. but it gives me a bunch of errors in red.
I've made the data1 and data 2 txt files in notepads, but i dunno why the errors say cannot find file.

> java MergeFiles
java.io.FileNotFoundException: File1.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at MergeFiles.main(MergeFiles.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.dynamicjava.symbol.JavaClass$JavaMethod.evaluate(JavaClass.java:326)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.handleMethodCall(ExpressionEvaluator.java:92)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.visit(ExpressionEvaluator.java:84)
at koala.dynamicjava.tree.StaticMethodCall.acceptVisitor(StaticMethodCall.java:105)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:38)
at edu.rice.cs.dynamicjava.interpreter.ExpressionEvaluator.value(ExpressionEvaluator.java:37)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:106)
at edu.rice.cs.dynamicjava.interpreter.StatementEvaluator.visit(StatementEvaluator.java:29)
at koala.dynamicjava.tree.ExpressionStatement.acceptVisitor(ExpressionStatement.java:101)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.evaluate(Interpreter.java:86)
at edu.rice.cs.dynamicjava.interpreter.Interpreter.interpret(Interpreter.java:47)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:205)
at edu.rice.cs.drjava.model.repl.newjvm.InterpreterJVM.interpret(InterpreterJVM.java:182)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
>


Whats wrong?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Merging two files [edited]

Posted 05 November 2009 - 10:00 PM

This is a topic already fully discussed (and solved) yesterday
Topic merged
please avoid double posting :angry:
Was This Post Helpful? 0
  • +
  • -

#9 starwok  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 20-October 09

Re: Merging two files [edited]

Posted 05 November 2009 - 10:25 PM

View Postpbl, on 5 Nov, 2009 - 09:00 PM, said:

This is a topic already fully discussed (and solved) yesterday
Topic merged
please avoid double posting :angry:



what the... it wasn't like this.. i think it was lag, made a double click. Sorry.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1