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

Create a new Project (Lab8) 1) Create a program that will load a file of unforma

ID: 3607271 • Letter: C

Question

Create a new Project (Lab8) 1) Create a program that will load a file of unformatted measurement values and re-write them to an output file as a formatted table a) Create a new package "task1" and a new class in that package "FileConverter". Add the empty "main" method in your class. b) Problem: o main: In the main, prompt the user to enter a file name, then read and save their input value Note: The provided input file name is "Input.txt", but any file name should be accepted · Call the "reWriteFile" method passing string file name. This method has a void return type so there is no need to save any returned value Done! o reWriteFile (accepts 1 parameter - a string file name) First, open the input file by creating a FilelnputStream object based on the provided input file name, then creating a Scanner object using that file input stream " " Then open the output file by creating a FileOutputStream object based on the provided file name, but with "_Converted" added to the file name. For example, "Input.txt" would become "Input Converted.txt"-this should work properly with any filename that ends in ".txt". Then create a PrintWriter object using that file name Write the table heading to the file: "Year For each line in the input file Jan Feb Dec " . Read the year value write that to the output file . Read 12 double values write each to the file (with 3 decimal places) Add a newline after the last of the 12 values - Close the scanner and the file input stream. Flush and close the print writer and the file output stream.

Explanation / Answer

//Creating a new package task1
package task1;

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

//Creating a new class FileConverter
class FileConverter {
  
public static void main(String args[]){
//Prompting and getting filename from user
Scanner scan = new Scanner(System.in);
System.out.print("Enter the filename: ");
String filename = scan.next();
//calling reWriteFile method
reWriteFile(filename);
scan.close();
}
//Method to get fileNameWithOutExt
static String stripExtension (String str) {
// Handle null case specially.

if (str == null) return null;

// Get position of last '.'.

int pos = str.lastIndexOf(".");

// If there wasn't any '.' just return the string as is.

if (pos == -1) return str;

// Otherwise return the string, up to the dot.

return str.substring(0, pos);
}
  
public static void reWriteFile(String filename){
  
try {
//Create FileInputStream
FileInputStream fileInStreamObj = new FileInputStream(filename);
InputStream inStreamObject = (InputStream) fileInStreamObj;
//Open File to read
Scanner sc = new Scanner( inStreamObject );
String data[] = new String[1000];
int line = 0;
//Reading eachLine and storing in String array
while(sc.hasNextLine()){
data[line++] = sc.nextLine();
}
sc.close();
inStreamObject.close();
fileInStreamObj.close();
  
//Getting filename with out extension
String fileNameWithOutExt = stripExtension (filename);
  
//Open file with new name(_Converted) to write
FileOutputStream fop = new FileOutputStream(new File(fileNameWithOutExt+"_Converted.txt"));
//Create PrintWriter Object
PrintWriter printWriter = new PrintWriter (fop);
//Writing Head line in file
printWriter.println("Year Jan Feb Mar Apr Jun Jul Aug Sep Oct Nov Dec");
  
for(int i=0;i<line;i++){
Scanner s = new Scanner(data[i]);
//Writing Year in output file
printWriter.print(s.nextInt()+" ");
//Writing 12 doubles in output file
double months[] = new double[12];
for(int j=0;j<12;j++){
printWriter.print(s.nextDouble()+" ");
}
printWriter.println();
s.close();
}
//flushing printWriter and closing
printWriter.flush();
printWriter.close ();
fop.close();
//Handle FileNotFoundException
} catch(FileNotFoundException e){
System.out.println("File not avaliable");
//Handle IOException
} catch(IOException e){
System.out.println("IOException araised");
}
}
}

/* Sample Input

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