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();
        }
    }
}

You need a method called main. Try changing

public static void MergeFiles

to

public static void main

and see if that works better.

Yea I've tried that also... but than it gives me a bunch of errors in red...
I made the textfiles thought, dunno why it says cannot find the 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)
>

java.io.FileNotFoundException: File1.txt (The system cannot find the file specified) the specified path to file is not correct. Java is not able to find the file in the path given which you want to open.

katharnakh

Chances are that Java is not looking for your text files where you think it is looking. I don't use Java when I can avoid it and I don't know what IDE you are using but if you have an IDE that has a debugger built into it use that to see where it is looking. Or else give the program a fully described path that includes all disk and folder information rather than assume the default location for files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.