Question

Program using java !! Create two files, file1.csv and file2.csv Write the following information in file1.csv:...

Program using java !!

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

Homework Answers

Answer #1

Java.io.PrintWriter class

this class is used to  Prints formatted representations of objects in a text-output stream.

used to write line from other file as a filestream.

Java.io.BufferedReader Class in Java

Reads text from a character-input stream as a buffering characters so as to provide as a text reading in a line mod

code

//file1.csv contains data 
/*  Apple 1.69 001
    Banana 1.39 002   */
//file2.csv contains data 
/*
Apple 1.69 001
Carrot 1.09 003
Beef 6.99 004
*/    
//file3.csv contains data 
/*
Apple 1.69 001
*/
//file name check.java 
import java.io.*; 
public class check
{ 
  public static void main(String[] args)throws Exception 
  { 
  //BufferedReader to read file as a stream
  //br.readLine is ude to read line from file as string 
 BufferedReader br1 = new BufferedReader(new FileReader("file1.csv")); 
  BufferedReader br2 = new BufferedReader(new FileReader("file2.csv")); 
  String st; 
  //print file1 data 
  System.out.println("---------------------");
   System.out.println("File1 contains DATA : ");
   System.out.println("---------------------");
  while ((st = br1.readLine()) != null) 
    System.out.println(st); 
    //print file2 data 
   System.out.println("---------------------");
   System.out.println("File2 contains DATA : ");
   System.out.println("---------------------");
    while ((st = br2.readLine()) != null) 
    System.out.println(st); 
    BufferedReader br11 = new BufferedReader(new FileReader("file1.csv")); 
  BufferedReader br22 = new BufferedReader(new FileReader("file2.csv"));
    String line1,line2;
    //   PrintWriter to create new file as file3.txt
    PrintWriter pw = new PrintWriter("file3.csv");
    line1 = br11.readLine();
    line2= br22.readLine();
    System.out.println("---------------------");
   System.out.println("File3 contains DATA : ");
   System.out.println("---------------------");
   //code to check line1 and line2 have same string 
   //line1.equals(line2) return true if they are the same string
    while(line1!=null && line2!=null)
    {
    if(line1.equals(line2))
    {System.out.println(line1);
    pw.println(line1);
    } 
    line1 = br11.readLine();
    line2= br22.readLine();
    }
     pw.flush(); 
          
        // closing resources 
        br11.close(); 
         br22.close(); 
         br1.close(); 
         br2.close(); 
        pw.close();
  }
  }
  

Steps to run

Step 1 : write file name check.java

step2 : type javac check.java in the terminal for compile

step3 : type java check to run the file

Note

avoid writing extra line as null values means write line up to which required line don't give extra line while creating

your file file1.csv and file2.csv other wise it will givw error as null pointer exception.

output is below;

Know the answer?
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
Similar Questions
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter feeds. At a high level, your program is going to perform the following: Read in two files containing twitter feeds. Merge the twitter feeds in reverse chronological order (most recent first). Write the merged feeds to an output file. Provide some basic summary information about the files. The names of the files will be passed in to your program via command line arguments. Use...
This program is in C++: Write a program to allow the user to: 1. Create two...
This program is in C++: Write a program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow...
In JAVA write the following program: Objective: Practice object-oriented principles by making two Peanut Butter and...
In JAVA write the following program: Objective: Practice object-oriented principles by making two Peanut Butter and Jelly Sandwiches. The program must create two sandwiches based on user input. The sandwich information for both must then print out their details and determine if the two sandwiches are equal. Requirements: Write a class called Bread with the following Instance Variables Name: The name brand of the bread. o   Calories: The number of calories per slice assumed to be between 50 and 250 inclusively....
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Using C++, write the following program: The Point.h file declares the class and you will create...
Using C++, write the following program: The Point.h file declares the class and you will create a Point.cpp that contains the implementation and a main() that instantiates the Point class to add additional tests to cover the various overloaded operators Use the Point.h file that is found below (This class contains a point on a plane and this class is going to contain a X coordinate and Y coordinate. The class is also going to contain a member function that...
CIST 2371 Introduction to Java Unit 03 Lab Due Date: ________ Part 1 – Using methods...
CIST 2371 Introduction to Java Unit 03 Lab Due Date: ________ Part 1 – Using methods Create a folder called Unit03 and put all your source files in this folder. Write a program named Unit03Prog1.java. This program will contain a main() method and a method called printChars() that has the following header: public static void printChars(char c1, char c2) The printChars() method will print out on the console all the characters between c1 and c2 inclusive. It will print 10...
Laboratory 4: Black Jack! Java API ArrayList Lab 04 Documentation Included matrix package "external" documentation Included...
Laboratory 4: Black Jack! Java API ArrayList Lab 04 Documentation Included matrix package "external" documentation Included Download Lab04.zip for all of the additional supporting files that you will need to compile and run. Extract the files into your working directory You can find the rules of blackjack all over the Internet. To get an idea of what you are trying to accomplish in this lab, I’ll demonstrate the final solution. The dealer stands on all 17s. Doubling after splitting is...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT