Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a text file named “input.txt” containing student data, which has the foll

ID: 3676481 • Letter: C

Question

Create a text file named “input.txt” containing student data, which has the following attributes separated by comma (,): Student_Id, student_name, gender, grade.

The input file must have at least 5 records.

Here is a sample input.txt file format:

Student_Id, student_name, gender, grade

1137156, Tom, male, A

1234568, Amy, female, A

Please write a program that reads the text file & extracts student data from it to a csv file named “output.csv”. The output file should contain the same attributes as the input file.

Important Instructions:

1. You can use an external package com.csvreader.CsvWrite.

2. Because com.csvreader.CsvWriter is an external package, so you must add package: com.csvreader.CsvWriter in your program before you use it. (Hint: You need to add this external jar file in java build path of your program and import package: com.csvreader.CsvWriter in your program)

Explanation / Answer

import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

import com.csvreader.CsvWriter;

/**
* @author Srinivas Palli
*
*/
public class WriteToCSV {
   /**
   * @param args
   */
   public static void main(String[] args) {
       try {
           Scanner scanner = new Scanner(new File("input.txt"));
           String outputFile = "output.csv";
           // before we open the file check to see if it already exists
           boolean alreadyExists = new File(outputFile).exists();
           // use FileWriter constructor that specifies open for appending
           CsvWriter csvOutput = new CsvWriter(
                   new FileWriter(outputFile, true), ',');
           // if the file didn't already exist then we need to write out the
           // header line
           if (!alreadyExists) {
               csvOutput.write("Student_Id");
               csvOutput.write("student_name");
               csvOutput.write("gender");
               csvOutput.write("grade");
               csvOutput.endRecord();
           }
           // else assume that the file already has the correct header line
           // write out a few records
           // read data from file and write to csv file

           while (scanner.hasNext()) {

               String lineArr[] = scanner.nextLine().split(",");
               csvOutput.write(lineArr[0]);
               csvOutput.write(lineArr[1]);
               csvOutput.write(lineArr[2]);
               csvOutput.write(lineArr[3]);
               csvOutput.endRecord();

           }
           csvOutput.close();

       } catch (Exception e) {
           // TODO: handle exception
           e.printStackTrace();
       }
   }
}

input.txt:
1137156, Tom, male, A
1234568, Amy, female, A

output.csv:
Student_Id,student_name,gender,grade
1137156,Tom,male,A
1234568,Amy,female,A

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote