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

PLEASE write and read files using try{ String filename = \"src/test.txt\"; for(S

ID: 3809340 • Letter: P

Question

PLEASE write and read files using try{
        String filename = "src/test.txt";
        for(String line : Files.readAllLines(Paths.get(filename))){
            System.out.println(line);
        }
    } catch (IOException ex){
        ex.printStackTrace();
    }
and

try{
            String filename = "src/example.txt";
            String textToWrite = "This will be written to the file";
            Files.write(Paths.get(filename), textToWrite.getBytes());
        }catch(IOException ex){
            ex.printStackTrace();
        }

Files.write(Paths.get(filename), textToWrite.getBytes(), StandardOpenOption.APPEND);

String allValues = "2016-12-06,109.949997,26075900";
    String[] data = allValues.split(",");

PLEASE answer the following JAVA question

Q)

static ArrayList<String> Q2(String filename) {

        You are given a file (filename) containing a different random phrase on each line. Return an ArrayList containing each phrase, but without the first word of each phrase. Example: If the files contains the 2 phrases "roofed crossover" and "beneficiary charles frederick worth" the ArrayList should contain "crossover" and "charles frederick worth"


        return null;
    }

Explanation / Answer

StringQ2.java

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;

public class StringQ2 {

   public static void main(String[] args) {
       String filename = "D:\test.txt";
       ArrayList<String> list = Q2(filename);
       System.out.println(list );
   }
   static ArrayList<String> Q2(String filename) {
//You are given a file (filename) containing a different random phrase on each line. Return an ArrayList containing each phrase, but without the first word of each phrase. Example: If the files contains the 2 phrases "roofed crossover" and "beneficiary charles frederick worth" the ArrayList should contain "crossover" and "charles frederick worth"
       ArrayList<String> list = new ArrayList<String>();
       try{
           Charset charset = Charset.forName("ISO-8859-1");
  
   for(String line : Files.readAllLines(Paths.get(filename), charset)){
// System.out.println(line);
   line = line.substring(line.indexOf(" "), line.length());
   list.add(line.trim());
   }

   } catch (IOException ex){
   ex.printStackTrace();
   }
return list;
}

}

Output:

[crossover, charles frederick worth]

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