Question: {JAVA} Hello! I need some help with working on a program that reads in a text file line by line, replaces all spaces with commas,
{JAVA}
Hello! I need some help with working on a program that reads in a text file line by line, replaces all spaces with commas, and writes new lines to a new file.
Instructions
Write a program WriteCSV.java that reads in a text file line by line, replaces all spaces with commas, and writes new lines to a new CSV file.
This program contains a template from which you will begin coding.
Program Requirements
(1) Your task is to fix all the "TODO"s in the following code.
// TODO import statements public class WriteCSV { public static void main(String[] args) { // Grading program needs hardcoded filename. Oh, well. " String inputFilename = "coords.txt"; String outputFilename = changeFileExtToCsv(inputFilename); // Open files Scanner input = //TODO: call method to open input file PrintWriter output = //TODO: call method to open output file // TODO: Read input line, replace all spaces with commas, // and write output line while (input.hasNextLine()) { } // TODO: close streams } /** * Changes file extension to ".csv" * @param filename * @return */ public static String changeFileExtToCsv(String filename) { return filename.substring(0,filename.lastIndexOf('.')) + ".csv"; } /** * Open input for reading * @param filename * @return */ public static Scanner openInput(String filename) { Scanner in = null; try { File infile = new File(filename); in = new Scanner(infile); } catch (FileNotFoundException e) { //e.printStackTrace(); System.out.println(filename + " could not be found"); System.exit(0); } return in; } /** * Open output for writing * @param filename * @return */ public static PrintWriter openOutput(String filename) { //TODO: Write method to open a PrintWriter ; use openInput() as a guide } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
