Please complete these files. There are two text files attached. Running the code should result in the creation of an output file ("PE_output.txt"). Please give full reasoning to the answer. Please also paste the full java code and please attach a screenshot of the successful program run.    package solution; import java.io.*; import java.util.Scanner; public class PE_1_problem { @SuppressWarnings("resource") public static String readFirstLetters(String filename) { String result = ""; File f = new File(filename); // Surround the next command inside a try/catch block, // as it is possible the file doesn't exist. try { Scanner fileContents = new Scanner(f); while (fileContents.hasNextLine()) { // 1. Get the next line and store in a variable // 2. Check the length of the line. If the line is not an // empty string, add the character at the start of the line // to the result string. // 3. Otherwise, add an empty space to the result string. } fileContents.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } return result; } public static String readNthChars (String filename) { String result = ""; // This is a very similar method, except that instead of // reading the first letter, you will read the nth character, where // 'n' is the same as the line number (using a numbering system // that starts at 0, so the first line has n = 0). Blank lines in // the text file should not be counted when determining line number, // but should still contribute spaces as in the method above. Spaces // within line should also NOT be counted, so you might want to use a // command such as the following in your code: // currentLine = currentLine.replaceAll("\\s", ""); // This command removes all the spaces in the String 'currentLine' // You should also use the command: // currentLine = currentLine.toUpperCase() at some point, so that the // result is formatted all in upper case letters. return result; } public static void main(String[] args) { System.out.println(readFirstLetters("PE_file_1.txt")); System.out.println(readNthChars("PE_file_2.txt")); // Your code here to write the two lines above to an output file ("PE_output.txt"). // Hint: You might want to start with: FileWriter myWriter = new FileWriter("PE_output.txt"); // You should then be able to use myWriter.write("some string here"); // To move to a new line, use: myWriter.write("\n"); // You should check your output file to see that you got the output you wanted. // To find your output file, look in your eclipse workspace. } }   PE_file_1.txt Can of Soup - 1 Sweet potatoes - 5 Ice cream - 1 gallon Swiss cheese - 1/2 pound Fish - 1.5 pounds Unsweetened soy milk Nuts, assorted - 1 can   PE_file_2.txt Can I write a functional Java program? Is it possible to get a CS internship? Am I a good programmer? Can someone explain what good coding style is? How is data science related to computer science? What attributes should a good programmer have? How can I become a better programmer? What will I do after this class? How do you learn how to debug a program? How much effort does it take to prep for a technical interview? How come I usually can't understand everything in my CS class? What do you consider the best advice for someone studying CS?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

 

Please complete these files. There are two text files attached. Running the code should result in the creation of an output file ("PE_output.txt"). Please give full reasoning to the answer. Please also paste the full java code and please attach a screenshot of the successful program run. 

 

package solution;

import java.io.*;
import java.util.Scanner;

public class PE_1_problem {

@SuppressWarnings("resource")
public static String readFirstLetters(String filename) {
String result = "";
File f = new File(filename);
// Surround the next command inside a try/catch block,
// as it is possible the file doesn't exist.
try {

Scanner fileContents = new Scanner(f);
while (fileContents.hasNextLine()) {
// 1. Get the next line and store in a variable

// 2. Check the length of the line. If the line is not an
// empty string, add the character at the start of the line
// to the result string.

// 3. Otherwise, add an empty space to the result string.
}
fileContents.close();

} catch (FileNotFoundException e) {

e.printStackTrace();
}

return result;

}

public static String readNthChars (String filename) {
String result = "";
// This is a very similar method, except that instead of
// reading the first letter, you will read the nth character, where
// 'n' is the same as the line number (using a numbering system
// that starts at 0, so the first line has n = 0). Blank lines in
// the text file should not be counted when determining line number,
// but should still contribute spaces as in the method above. Spaces
// within line should also NOT be counted, so you might want to use a
// command such as the following in your code:

// currentLine = currentLine.replaceAll("\\s", "");

// This command removes all the spaces in the String 'currentLine'

// You should also use the command:

// currentLine = currentLine.toUpperCase() at some point, so that the
// result is formatted all in upper case letters.


return result;
}

public static void main(String[] args) {

System.out.println(readFirstLetters("PE_file_1.txt"));
System.out.println(readNthChars("PE_file_2.txt"));

// Your code here to write the two lines above to an output file ("PE_output.txt").
// Hint: You might want to start with: FileWriter myWriter = new FileWriter("PE_output.txt");
// You should then be able to use myWriter.write("some string here");
// To move to a new line, use: myWriter.write("\n");

// You should check your output file to see that you got the output you wanted.
// To find your output file, look in your eclipse workspace.
}

}

 

PE_file_1.txt

Can of Soup - 1
Sweet potatoes - 5

Ice cream - 1 gallon
Swiss cheese - 1/2 pound

Fish - 1.5 pounds
Unsweetened soy milk
Nuts, assorted - 1 can

 

PE_file_2.txt

Can I write a functional Java program?
Is it possible to get a CS internship?

Am I a good programmer?
Can someone explain what good coding style is?

How is data science related to computer science?
What attributes should a good programmer have?
How can I become a better programmer?
What will I do after this class?
How do you learn how to debug a program?

How much effort does it take to prep for a technical interview?
How come I usually can't understand everything in my CS class?
What do you consider the best advice for someone studying CS?

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 6 images

Blurred answer
Knowledge Booster
Constants and Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education