Question

In: Computer Science

Create two files, file1.csv and file2.csv Write the following information in file1.csv: Apple 1.69 001 Banana...

Create two files, file1.csv and file2.csv

Write the following information in file1.csv:

Apple 1.69 001

Banana 1.39 002

Write the following information in file2.csv:

Apple 1.69 001

Carrot 1.09 003

Beef 6.99 004

You have two files, both of the two files have some information about products: • Read these two files, find out which products exist in both files, and then save these products in a new file. You can use the two files you created in Lab 1 as an example. If you use file1.csv and file2.csv from Lab 1, the result file will be:

•Result file:

Apple 1.69 001

8.5.0

Solutions

Expert Solution

JAVA Program to read two files, finding similar products that exist in both files and save it in a new file :

import java.io.File;//Importing all required modules
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Scanner;

class JavaEg {
    public static void main(String[] args) throws IOException {
        
        Scanner input1 = new Scanner(new File("file1.csv"));//reading first file
        Scanner input2 = new Scanner(new File("file2.csv"));//reading second file

while(input1.hasNextLine() && input2.hasNextLine()){
    String first_file = input1.nextLine();   //reading lines on first file
    String second_file = input2.nextLine(); //reading lines on second file

    if(first_file.equals(second_file)){   //comparing lines of both files
        System.out.println("Similarities found: "+"\n"+first_file);
        String content = first_file+"\n";
        String path = "File3.csv"; //path of new output file
        try { //using try to write 
            Files.write(Paths.get(path), content.getBytes(), StandardOpenOption.APPEND); //writing similar contents to anew output file
        }catch (IOException e) { // using catch for exception
        }
    }
}
    }
}

File 1:

File 2:

Output File3:

The answer have been updated in Java language.


Related Solutions

Write a bash script to download two files, say file1.gz and file2.gz from a given web...
Write a bash script to download two files, say file1.gz and file2.gz from a given web address to the directory "assignment5"
Edit question Write a program that merges two files as follows. The two files are in...
Edit question Write a program that merges two files as follows. The two files are in the docsharing which you can download it. One file will contain usernames(usernames.txt):foster001smith023nyuyen002...The other file will contain passwords(passwords.txt):x34rdf3ep43e4rddw32eds22...The program should create a third file matching username and passwords(usernamesPasswords.txt):foster001x34rdf3esmith023p43e4rddnyuyen002w32eds22......Give the user of your programs the option of displaying you output file. CAN ANYONE SOLVE THIS IN C
In JAVA : There are two text files with the following information stored in them: The...
In JAVA : There are two text files with the following information stored in them: The instructor.txt file where each line stores the id, name and affiliated department of an instructor separated by a comma The department.txt file where each line stores the name, location and budget of the department separated by a comma You need to write a Java program that reads these text files and provides user with the following menu: 1. Enter the instructor ID and I...
C++ Create an ArrayBag template class from scratch. This will require you to create two files:...
C++ Create an ArrayBag template class from scratch. This will require you to create two files: ArrayBag.hpp for the interface and ArrayBag.cpp for the implementation. The ArrayBag class must contain the following protected members: static const int DEFAULT_CAPACITY = 200; // max size of items_ ItemType items_[DEFAULT_CAPACITY]; // items in the array bag int item_count_; // Current count of items in bag /** @param target to be found in items_ @return either the index target in the array items_ or...
Create files with the following names : test1, test2, test3. Create a symbolic link to test2...
Create files with the following names : test1, test2, test3. Create a symbolic link to test2 and name it test4 Create a directory and name it test5. Write a shell script to perform the following tasks: • check if a file named test6 exist. If not, it should create it. • Display a text to indicate whether test4 is symbolic link or not • Display a text to indicate whether test2 is directory or not • Display a text to...
Create a program RandomFile.java that generates files with random numbers/characters. You are required to write the...
Create a program RandomFile.java that generates files with random numbers/characters. You are required to write the following three methods in the class RandomFile: ​public static void randomBinaryFile(String fileName, int length) ​public static void randomNumberFile(String fileName, intlength) ​public static void randomCharFile(String fileName, int length) The parameter “fileName” specifies the file name, and “length” specifies the length of the sequence in the file. ● randomBinaryFile will generate a file containing a sequence of 0 or 1 of the specified length; ● randomNumberFile...
File Compare Write a program that opens two text files and reads their contents into two...
File Compare Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. // Copyright (c) 2013 __Pearson...
 In this exercise, you will create two external style sheet files and a web page. You...
 In this exercise, you will create two external style sheet files and a web page. You will experiment with linking the web page to the external style sheets and note how the display of the page is changed. Create an external style sheet (call it format1.css) to format as follows: document background color of white, document text color of #000099, and document font family of Arial, Helvetica, or sans-serif. Hyperlinks should have a background color of gray (#CCCCCC). Configure the...
The Matisse Co. produces banana cakes. The following information relates to the actual production of 4,000...
The Matisse Co. produces banana cakes. The following information relates to the actual production of 4,000 banana cakes. Standards: ·         Direct materials cost $2.00 per pound & each cake requires 1 pound of bananas. ·         Direct labor cost $11.00 per hour & each cake will take 1 DLH to complete. Actual: ·         Bananas $1.90 per pound. 4,500 pounds of bananas were purchased and used. ·         Actual direct labor cost was $43,460 at a cost of $10.60 per DLH. What is the direct materials quantity...
When comparing two bonds the following statement is not true for a comparable "apple to apple"...
When comparing two bonds the following statement is not true for a comparable "apple to apple" comparison Should have the same term to maturity Should have the same risk class Can have different coupons Can have different yields. Can have different prices
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT