Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Input: A string of the csv filename (with .csv extension) that has 2 columns.
- * Output: A multidimensional array of strings, where the first index is the abbreviation,
- * the second index is the full value
- * O(m) Runtime, where m is the number of abbreviations
- * O(2m) -> O(m) Storage, where m is the number of abbreviations
- *
- * CAN ONLY HANDLE 100 Abbreviations
- */
- public static String[][] csvToArray(String csvName) {
- String[][] csvArray = new String[100][2]; // create a multidimensional array that can hold 100 abbreviations
- try {
- BufferedReader reader = new BufferedReader(new FileReader(csvName));
- reader.readLine(); // read first line to remove column titles
- String row;
- int index = 0;
- // O(m) runtime, where m is the number of lines(abbreviations)
- while ((row = reader.readLine()) != null) { // read until end of file
- String[] kv = row.split(","); // split the row into col 1 and 2 based on ,
- csvArray[index][0] = kv[0]; // set first index to the abbreviation
- csvArray[index][1] = kv[1]; // set second index to the full value
- index++;
- }
- return csvArray;
- // Catch Exceptions
- } catch(FileNotFoundException e) {
- System.out.println(String.format("Cannot Find %s", csvName));
- } catch (IOException e) {
- System.out.println("Error Reading File");
- } catch (IndexOutOfBoundsException e) {
- System.out.println("Too Many Abbreviations");
- }
- // If exception is caught, set first index to null
- csvArray[0][0] = null;
- return csvArray;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement