Question

Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed as command-line arguments to MergeF
1 0
Add a comment Improve this question Transcribed image text
Answer #1

PLEASE GIVE IT A THUMBS UP

-/Desktop/CODE/A.java (CODE) - Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Project Preferences Help

import java.util.*;
import java.lang.*;
import java.io.*;
class MergeFiles{

   public static void removeDuplicates(Vector v)
{
for(int i=0;i<v.size();i++)
for(int j=0;j<v.size();j++)
if(i!=j)
if(v.elementAt(i).equals(v.elementAt(j)))
v.removeElementAt(j);
}

   public static void main(String[] args) throws Exception {
       Vector<String> name = new Vector<String>();      
       File f = new File(args[0]+".txt");
       BufferedReader br = new BufferedReader(new FileReader(f));      
       while(true){
           String s = br.readLine();
           if(s==null)
               break;
           name.add(s);
       }
       br.close();
       f = new File(args[1]+".txt");
       br = new BufferedReader(new FileReader(f));      
       while(true){
           String s = br.readLine();
           if(s==null)
               break;
           name.add(s);
       }
       br.close();
       Collections.sort(name);
       removeDuplicates(name);
       f = new File(args[2]+".txt");
       BufferedWriter bw = new BufferedWriter(new FileWriter(f));      
       for(int i=0;i<name.size();i++)
           bw.write(name.elementAt(i)+"\n");
       bw.close();
   }
}

nikhil@nikhil-Vostro-15-3568:-/Desktop/CODE$ javac A.java nikhil@nikhil-Vostro-15-3568:-/Desktop/CODE$ java MergeFiles file1

Open # Merge File.txt -/Desktop/CODE Save = 0 layush himanshi hrithik meghu nikhil saksham sid srishti Plain Text Tab Width:

Open file 1.txt -/Desktop/CODE Save nikhil saksham hrithik ayush meghu Plain Text Tab Width: 8 Ln 5, Col 6 INS

nikhil
saksham
hrithik
ayush
meghu

Open File 2.txt -/Desktop/CODE Save 3 00 srishti himanshi sid nikhil saksham Plain Text Tab Width: 8 Ln 5, Col 8 INS

srishti
himanshi
sid
nikhil
saksham

Add a comment
Know the answer?
Add Answer to:
Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This assignment uses functions, files, and strings. Enough flexibility is provided for you to apply your...

    This assignment uses functions, files, and strings. Enough flexibility is provided for you to apply your knowledge of basic C++ programing to develop your solution. Develop a functional flowchart and then write a C++ program to solve the following problem. 1. Create a text file named file1.txt and write your brand of car (like Honda, Toyota, etc) in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your...

  • Q22 Consider the following 2 text files and Java program. 8 Points Answer the following questions...

    Q22 Consider the following 2 text files and Java program. 8 Points Answer the following questions keeping in mind that: • All three files are in the same directory, and there are no other files. • The program may crash. If the program crashes, answer the question with: "The program crashes" standings1.txt: 1 Valtteri Bottas 25 2 Charles Leclerc 18 3 Lando Norris 16 4 Lewis Hamilton 12 standings2.txt: 1 Valtteri Bottas 43 2 Lewis Hamilton 37 3 Lando Norris...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • **Java** Assume that indata1 and indata2 are two files containing at least two integers, separated by...

    **Java** Assume that indata1 and indata2 are two files containing at least two integers, separated by white space. Write a program named Add2 that writes the sum of the first integers of these two files to a file named outdata1. It writes the sum of the first integers of these two files to a file named outdata2. Each sum is written on a line by itself. So, if the contents of indata1 were "37 6 90" and the contents of...

  • Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

    Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...

  • i have to write a program that asks the names of 2 files. the first should...

    i have to write a program that asks the names of 2 files. the first should be open for reading second for writing. the program should read the the contents of the first file change all characters to uppercase and store in the second file. the second file will be a copy of the first file, except that all characters will be uppercase. use notepad to test the program. i got this so far {         String firstfile;         String...

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...

    Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...

  • The files must be called Proper coding conventions required the first letter of the class start...

    The files must be called Proper coding conventions required the first letter of the class start with a capital letter and the first letter of each additional word start with a capital letter. Only submit the .java file needed to make the program run. Do not submit the .class file or any other file. 5% Style Components Include properly formatted prologue, comments, indenting, and other style elements as shown in Chapter 2 starting page 64 and Appendix 5 page 881-892....

  • Using Unix processes Submit a README file that lists the files you have submitted along with...

    Using Unix processes Submit a README file that lists the files you have submitted along with a one sentence explanation. Call it Prj1README. MakeCopy.c : Write a C program that makes a new copy of an existing file using system calls for file manipulation. The names of the two files and copy block sizes are to be specified as command line arguments. Open the source file in read only mode and destination file in read/write mode. ForkCopy.c : Write a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT