Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
|
| Programming Exercises |
| 12.1 | Write a program to grep a text file, i.e. find all matches for the given regular expression in the text file. The program provides a test file, but ideally the file name and the regular expression should be specified on the command line. Given the test data below, the program prints:
1: [(3,4:be), (16,17:be)] 3: [(2,3:be)] 4: [(0,1:Be), (12,13:be), (22,23:be), (35,36:Be)] A skeleton for the program is given below.
Code View:
Scroll
/
Show All import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class VerySimpleGrep {
public static void main(String[] args) throws IOException {
// Test file
FileWriter fw = new FileWriter("test.txt");
fw.write("To be or not to be.\n");
fw.write("Only A's, no B's.\n");
fw.write("A bee movie is not funny.\n");
fw.write("Bessy was a beautiful beatnik from Bern.\n");
fw.close();
String fileName = "test.txt";
String regexStr = "[bB]e";
grepFile(fileName, regexStr);
}
/**
* Finds and prints matches for the regex in the text file.
* @param fileName
* @param regex
* @throws IOException
*/
public static void grepFile(String fileName, String regex)
throws IOException {
// ...
}
/**
* Finds the matches for the pattern in the target.
* @param pattern
* @param target
* @return List<String> with the matches found in the target
*/
public static List<String> grepLine(Pattern pattern, String target) {
// ...
}
}
|
| 12.2 | Write a method that reads input specified in Comma-Separated-Value (CSV) format. Each line represents a record with a fixed number of fields. The fields in each record are separated by a comma. The last field of the record is terminated by a line separator.
The method reads the input one line at a time, and extracts the field values. The program must ensure that the correct number of fields are specified in each record. Given the test data below, the program prints: [2.5, 25, 250] [Hi, Hello, Howdy] [2008, 2009, 2010] [one, two, three] A skeleton for the program is given below.
Code View:
Scroll
/
Show All import static java.lang.System.out;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Pattern;
public class CSVReader {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("csv.txt");
fw.write("2.5,25,250\n");
fw.write("Hi,Hello,Howdy\n");
fw.write("2008,2009,2010\n");
fw.write("one,two,three\n");
fw.close();
BufferedReader source = new BufferedReader(new FileReader("csv.txt"));
readCSV(source, 3);
source.close();
}
/**
* Reads values in CSV format.
* @param source
* @param numOfFields
* @throws IOException
*/
public static void readCSV(Readable source,
int numOfFields)throws IOException {
// ...
}
/**
* Creates a pattern that corresponds to the number of fields
* specified in CSV format on each line/record.
* @param numOfFields
* @return Pattern
*/
public static Pattern compileCSVPattern(int numOfFields) {
// ...
}
}
|