Add comments for CSVUtility.java
This commit is contained in:
parent
0350952979
commit
5a270cf719
Binary file not shown.
Binary file not shown.
|
@ -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
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
|
Loading…
Reference in New Issue