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

1e import java.io.IoException; 2 import java nio file iles 3 import java nio. fi

ID: 3800590 • Letter: 1

Question

1e import java.io.IoException; 2 import java nio file iles 3 import java nio. file. Paths; 4 import java util.ArrayList; 6 public class FilesLecture2 9 2 points 10e static string Q1 (String commaSeparatedValues) 11 TODO: You are give a string containing at least 2 comma separated values. Return the second value in a 12 separate string. 13 14 15 return 16 17 18 19 20 3 points 21e static ArrayList Q2 (String csvFilename) TODO: Read the given file which contains comma separated values and return an ArrayList containing only the 23 first value from each line 24 25 26 return null 27 28

Explanation / Answer

FileLecture2.java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;


public class FileLecture2 {

   public static String Q1(String commaSeparatedValues){
       String values[] = commaSeparatedValues.split(",");
       return values[1];
   }
   public static ArrayList<String> Q2(String csvFileName){
       String line = "";
       ArrayList<String> list = new ArrayList<String>();
       try {
           BufferedReader br = new BufferedReader(new FileReader(csvFileName)) ;

   while ((line = br.readLine()) != null) {

   String[] values = line.split(",");
   list.add(values[0]);
  
   }

   } catch (IOException e) {
   e.printStackTrace();
   }
   return list;
   }
   public static void main(String[] args) {
       String secondValue = Q1("MSFT,2016-09-23,57.43");
       System.out.println("Expecting: 2016-09-23");
       System.out.println("Returned: "+secondValue);
       System.out.println();
       ArrayList<String> result = Q2("D:\testFile.csv");
       System.out.println(result);

   }

}

OUtput:

Expecting: 2016-09-23
Returned: 2016-09-23

[1, 2, 3]

testFile.csv

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