In this lab you will write a program to read in a table of data from a text file where each line represents a record, and manipulate it. Each line of the input file will consist of sequence of quoted strings (using double quotation marks) separated by commas.Each quoted string will not contain any double quotation marks.Each line will contain the same number of quoted strings as any other.Here is a useful test file containing such data.
"1997","Ford","E350","ac, abs, moon","3000.00"
"1999","Chevy","Venture Extended Edition","","4900.00"
"1999","Chevy","Venture Extended Edition, Very Large","","5000.00"
"1996","Jeep","Grand Cherokee","MUST SELL! air, moon roof, loaded","4799.00"
Step 1. In a file Text1.java write the class Text1, a main method and a method with the following signature.
private static String[] read_record(Scanner in)
Don't forget theimportdirective for the Scanner class at the top of your program.
This method should read a line of text from the Scanner if there is one. (If there is no line to be read it should returnnull.) Then it should create String objects corresponding to the text inside each quoted string in that line, and return a new array of Strings containing these in the order they occurred on the line. The array returned should be of exactly the right size. You may assume that there are fewer than 1000 such quoted strings on a line.
Test your method by writing a loop in the main program to write the strings one-per-line in the array returned by a single call toread_record.
You need to be careful that some of the quoted strings may themselves contain white space, so merely replacing the double quotation marks and the commas with a white space character and then breaking up the line into tokens will not work. The quoted strings may also contain commas, so similar remarks apply for those. You may find methods in the String class to be useful. Do not use any other classes that come with Java apart from those taught in this course.
Step 2.Copy Text1.java to Text2.java and rename the class Test1 to Test2. Now modify your program to receive an input filename and an output filenameon the command lineused to execute Text2.java, and open them for input and output. Write a method with the following signature.
private static String entab_record(String[] rec)This method should produce a String consisting of the strings in the array rec concatenated together with a tab character between each string and the following one. Ensure that there is no trailing tab after the last string to come from the array. (Tabs are to be used here in the same way that commas were used in the original input, to separate the items of data on a line, only now the data must not be quoted.)
Now write a main program to read each line withread_recordand convert the resulting array into a String withentab_record, and then write it out followed by a newline, so that the result of running your program is to convert the original file to one containing the same data on each line, just formatted with tabs separating the data instead of quoted with commas separating the data.