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
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class InClassExercisee {
public static void main(String[] args) throws IOException {
Scanner inputfile = new Scanner(new File("data.csv"));
while (inputfile.hasNextLine()) {
String scan1 = inputfile.next();
scan1 = scan1.replaceAll("[\\| ]", " ");
String[] parts = inputfile.nextLine().split("|", 2);
for (String p : parts) {
System.out.println(p);
}
scan1 = scan1.replaceAll("[\\| ]", " ");
String scan2 = inputfile.next();
scan2 = scan2.replaceAll("[\\|]", " ");
String scan3 = inputfile.next();
scan3 = scan3.replaceAll("[\\| ]", ", ");
System.out.println(scan1 + "," + scan2 + ","
+ scan3 + inputfile.nextLine());
}
}
}
OUTPUT FOR THE PROGRAM ABOVE is:
Fred Flintstone,1212 Bedrock,Austin, Texas
Harry Potter,1234 Hogwarts,Road
Somewhere Overthere,Ugmal Smith,111, On the Sea
London England,Udlal Hayes,0000, Firston, Block
Harrison Alabama,Bifund Frediburg,1, Small Street
Shire
Hobbiton The,Zaghim Zagg,A-1, Another Street
Wormwood Mordor,Kari Underwood,1234, Song Street
Nashville Tennessee,Fari Away,A323, Desire
Houston Texas,Thudu Ofthewood,555, NotANumberedStreet Street
Snook Texas,Gimli Axthrower,123, Dairy Lane
Lonely Mountain|England
The,Throri Mybrother,8088, Java Lane
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at InClassExercisee.main(InClassExercisee.java:22)
BUT I WANTED IT TO LOOK LIKE THIS:
Fred Flintstone,1212 Bedrock,Austin, Texas
Harry Potter,1234 Hogwarts,Road, Somewhere, Overthere
Ugmal Smith,111 On the Sea, London, England
Udlal Hayes, 0000 Firston, Block, Harrison, Alabama