diff options
author | ArghKevin <kagheli@student.sdccd.edu> | 2024-05-20 05:42:37 -0700 |
---|---|---|
committer | ArghKevin <kagheli@student.sdccd.edu> | 2024-05-20 05:42:37 -0700 |
commit | 583ecf2df556c0bb3ae5468a63d9231b3ef56f58 (patch) | |
tree | 32aadcfbd21d79e34eb7d228f8b6e76dce9c0158 /src/CSVReader.java | |
parent | ae99f9afc615bfd6f4dd0b6d6f7847928956a318 (diff) |
Weeks 5 and 6.
Diffstat (limited to 'src/CSVReader.java')
-rw-r--r-- | src/CSVReader.java | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/src/CSVReader.java b/src/CSVReader.java index fa588ef..3666531 100644 --- a/src/CSVReader.java +++ b/src/CSVReader.java @@ -17,14 +17,15 @@ import java.util.Arrays; */ public class CSVReader extends Reader { - String[] header; // A CSV file has-a header. One line, multiple fields. - /* Array of Hash map from header to value for each - line past the header. */ - ArrayList<HashMap<String,String>> body; // A CSV file has-a body. Multiple lines, multiple fields. + private String[] header; // A CSV file has-a header. One line, multiple fields. + // Array of HashMaps. Headers are keys, values are on each line of the CSV following the header. + private ArrayList<HashMap<String,String>> body; // A CSV file has-a body. Multiple lines, multiple fields. public CSVReader(File file) { super(file); // Call parent constructor + /* Construct the body to be filled. */ body = new ArrayList<HashMap<String,String>>(); + /* Parse the contents of the file, store in body. */ parse(); } @@ -32,26 +33,46 @@ public class CSVReader extends Reader { * Parse CSV file. */ public void parse() { + /* Split the input on newlines. */ String[] lines = this.getContents().split("\n"); + /* Split the first line by commas, assign to header array. */ this.header = lines[0].split(","); - /* Iterate over all lines of the body. */ + /* Iterate over all lines of the body. + First line is header, count from 1 instead of 0. */ for (int i = 1; i < lines.length; i++) { + /* Some lines have commas within parentheses. + Angkor,"/South East Asian (Thai, Khmer, Lao)/Looped",100 + Remove everything within parentheses. We need to escape + not on a language level, but on a function call level. + Double-backslash for a literal backslash, which is then + used to escape the parentheses in the regex library. */ + lines[i] = lines[i].replaceAll("\\(.*\\)", ""); + + /* CSV, Comma-Separated Values. */ String[] fields = lines[i].split(","); + /* Allocate a new HashMap for the given line of the CSV. */ HashMap<String,String> map = new HashMap<String,String>(); - /* For each of the columns, add the associated - value in this row. */ + + /* Associate each of the line's fields with its header. */ for (int j = 0; j < header.length; j++) { map.put(header[j], fields[j]); } + /* Add the HashMap to the list. */ body.add(map); } } + /** + * @return the length of the body. + */ public int getLength() { return body.size(); } + /** + * @return the value associated with the given key on the given line. + */ public String get(int i, String key) { return body.get(i).get(key); } |