diff --git a/Week9/Assignment9/bin/CSVUtility.class b/Week9/Assignment9/bin/CSVUtility.class index 904cdf1..1f8f917 100644 Binary files a/Week9/Assignment9/bin/CSVUtility.class and b/Week9/Assignment9/bin/CSVUtility.class differ diff --git a/Week9/Assignment9/bin/CSVUtilityTest.class b/Week9/Assignment9/bin/CSVUtilityTest.class index 608b8af..036f825 100644 Binary files a/Week9/Assignment9/bin/CSVUtilityTest.class and b/Week9/Assignment9/bin/CSVUtilityTest.class differ diff --git a/Week9/Assignment9/src/CSVUtility.java b/Week9/Assignment9/src/CSVUtility.java index a5fef88..73faa31 100644 --- a/Week9/Assignment9/src/CSVUtility.java +++ b/Week9/Assignment9/src/CSVUtility.java @@ -19,15 +19,15 @@ public class CSVUtility { // ENTER CODE HERE Scanner sc = new Scanner(new File(filename)); // Create a Scanner to take in the file at path filename int length = 0; //Create a length variable and initialize it to zero - for (length = 0; sc.hasNext(); length++) - sc.nextLine(); + for (length = 0; sc.hasNext(); length++) //Find Line amount | Loop until there's no next line,each loop length++ + sc.nextLine();// Go to the next line sc.close(); //Close the Scanner to prevent resource leak - String[] filedata = new String[length]; + String[] fileData = new String[length]; //create a 1D array to store file data sc = new Scanner(new File(filename)); //Reinitialize the closed scanner for (int i = 0; i < length; i++) - filedata[i] = sc.nextLine(); + fileData[i] = sc.nextLine(); //Send line i to array fileData at index i-1 sc.close(); //Close the Scanner to prevent resource leak - return filedata; //Return + return fileData; //Return array filled with file data } /** @@ -41,15 +41,12 @@ public class CSVUtility { */ public static String[][] readCSV(String filename, boolean header) throws FileNotFoundException { // ENTER CODE HERE - String[] data = readFile(filename); - int lineOffset = header ? 1 : 0; - int maxElement = 0; - for (int i = lineOffset; i < data.length; i++) - maxElement = Math.max(data[i].split(",").length, maxElement); - String[][] csv = new String[data.length - lineOffset][maxElement]; - for (int i = lineOffset; i < data.length; i++) - csv[i - lineOffset] = data[i].split(","); - return csv; + String[] data = readFile(filename); //Read the file + int lineOffset = header ? 1 : 0; //do we have header? if yes, start at line 2 + String[][] csv = new String[data.length - lineOffset][]; + for (int i = lineOffset; i < data.length; i++) //Go through all rows + csv[i - lineOffset] = data[i].split(","); //Array.split(String delimiter) return a 1D array with each element seperated by the delimiter + return csv; //Send back the CSV file } diff --git a/Week9/Assignment9/src/CSVUtilityTest.java b/Week9/Assignment9/src/CSVUtilityTest.java index d092883..342b695 100644 --- a/Week9/Assignment9/src/CSVUtilityTest.java +++ b/Week9/Assignment9/src/CSVUtilityTest.java @@ -26,6 +26,10 @@ public class CSVUtilityTest { ArrayUtility.show(CSVUtilityGiven.readCSV("course-db.csv", true)); System.out.println("Your result:"); ArrayUtility.show(CSVUtility.readCSV("course-db.csv", true)); + System.out.println("----------------------------------------"); + ArrayUtility.show(CSVUtilityGiven.readCSV("uneven.csv", false)); + System.out.println("Your result:"); + ArrayUtility.show(CSVUtility.readCSV("uneven.csv", false)); } } diff --git a/Week9/Assignment9/uneven.csv b/Week9/Assignment9/uneven.csv new file mode 100644 index 0000000..b7f5a4a --- /dev/null +++ b/Week9/Assignment9/uneven.csv @@ -0,0 +1,10 @@ +uneven +Player01,4,4,6,3,4,6,3,5,4,6,4,4,3,6 +Player02,4,4,4,5 +Player03,4,5,5,4,6,5,5,5,7,6,3,6,4,6,5,5,4,5 +Player04,5,6,4,3,5,4,7,4,6 +Player05,4,3,4,4,4,5,4,4,5,5,4,5,5,5,5,4,5,4 +Player06,3,4,4,3,3,4,4,4,5,5,3,4 +Player08,4,6,5,3,4,6,4,3,4,4,2,5,4,4,3,4 +Player09,5,5,8,4,5,4,5,3,6,6,5,4,5 +Player10,5,4,5,3,6,4,5,4,6,5,5