This code is to be written in Java You are given a file containing the names and
ID: 3670330 • Letter: T
Question
This code is to be written in Java
You are given a file containing the names and addresses of company employees from many years ago that your manager has asked you to import into a database. You can use a CSV file and your database application to load the file, but the file your manager gave you was exported from an old, non-standard accounting system. Here is its format:
Freddy|Kruger
1313|Mockingbird|Lane
Houston|Texas
Billy|Thornton
1010|Slingblade|Street
Houston|Texas
Write a program that reads in the file and exports it to a standard CSV format. For the records above, the output format would be:
Freddy Kruger,1313 Mockingbird Lane, Houston, Texas
Billy Thornton,1010 Slingblade Street, Houston, Texas
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Fileio {
public static void main(String[] args) throws FileNotFoundException {
Scanner s1 = new Scanner(new File("inputfile.txt"));
while (s1.hasNextLine()) {
String name = s1.next();
name = name.replaceAll("[\| ]", " ");
String address = s1.next();
String[] parts = s1.nextLine().split("|",2);
for(String p: parts) {
System.out.println(p);
}
address = address.replaceAll("[\|]", " ");
String area = s1.next();
area = area.replaceAll("[\| ]", ",");
System.out.println(">>"+name + "," + address + "," + area);
}
s1.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.