From 021fb0e4b5eecf5771cb7d060628adab35286946 Mon Sep 17 00:00:00 2001 From: Maxwell Maslov Date: Tue, 15 Oct 2024 14:21:10 -0700 Subject: [PATCH 1/3] Debugged dta and mp1, and renamed them to followable variable names. --- src/Main.java | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Main.java b/src/Main.java index e5571ea..a7e0262 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,34 +6,34 @@ public class Main { public static void main(String[] args) { - Scanner s = new Scanner(System.in); + Scanner s = new Scanner(System.in); // instantiating new scanner object - System.out.print("Enter the CSV filename: "); + System.out.print("Enter the CSV filename: "); // prompting the user for file input String f = s.nextLine(); - List> dta = new ArrayList<>(); - try (Scanner fs = new Scanner(new File(f))) { + List> structureMap = new ArrayList<>(); // creates a map to structure the CSV columns + try (Scanner fs = new Scanner(new File(f))) { fs.nextLine(); while (fs.hasNextLine()) { - String[] v = fs.nextLine().split(","); + String[] v = fs.nextLine().split(","); // seperating by commas int chg = Integer.parseInt(v[2]); - Map mp1 = new HashMap<>(); - mp1.put("id", v[0]); - mp1.put("tm", v[1]); - mp1.put("chg", String.valueOf(chg)); - dta.add(mp1); + Map fileDataMap = new HashMap<>(); // placing commit statistics into a new hashmap + fileDataMap.put("id", v[0]); + fileDataMap.put("tm", v[1]); + fileDataMap.put("chg", String.valueOf(chg)); + structureMap.add(fileDataMap); } } catch (FileNotFoundException e) { - System.out.println("Error reading the file: " + e.getMessage()); + System.out.println("Error reading the file: " + e.getMessage()); // if there is no file, throw exception s.close(); return; } - Map>> mp2 = new HashMap<>(); - for (Map d : dta) { + Map>> mp2 = new HashMap<>(); // populating the mp2 hashmap with file content + for (Map d : structureMap) { String id = d.get("id"); List> lst = mp2.get(id); if (lst == null) { @@ -50,7 +50,7 @@ public static void main(String[] args) { List> sel; if (inp.equalsIgnoreCase("all")) { - sel = dta; + sel = structureMap; } else { String id = "fork" + inp; sel = mp2.get(id); From dbb593322b8fb117b2807728e189752ee119decd Mon Sep 17 00:00:00 2001 From: Alexander Ruban Date: Tue, 15 Oct 2024 14:47:12 -0700 Subject: [PATCH 2/3] renamed variables --- src/Main.java | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/Main.java b/src/Main.java index a7e0262..3ae8312 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,54 +6,54 @@ public class Main { public static void main(String[] args) { - Scanner s = new Scanner(System.in); // instantiating new scanner object + Scanner scanner = new Scanner(System.in); // instantiating new scanner object System.out.print("Enter the CSV filename: "); // prompting the user for file input - String f = s.nextLine(); + String fileName = scanner.nextLine(); List> structureMap = new ArrayList<>(); // creates a map to structure the CSV columns - try (Scanner fs = new Scanner(new File(f))) { - fs.nextLine(); + try (Scanner fileScanner = new Scanner(new File(fileName))) { + fileScanner.nextLine(); - while (fs.hasNextLine()) { - String[] v = fs.nextLine().split(","); // seperating by commas + while (fileScanner.hasNextLine()) { + String[] values = fileScanner.nextLine().split(","); // seperating by commas - int chg = Integer.parseInt(v[2]); + int changes = Integer.parseInt(values[2]); Map fileDataMap = new HashMap<>(); // placing commit statistics into a new hashmap - fileDataMap.put("id", v[0]); - fileDataMap.put("tm", v[1]); - fileDataMap.put("chg", String.valueOf(chg)); + fileDataMap.put("id", values[0]); + fileDataMap.put("tm", values[1]); + fileDataMap.put("chg", String.valueOf(changes)); structureMap.add(fileDataMap); } - } catch (FileNotFoundException e) { - System.out.println("Error reading the file: " + e.getMessage()); // if there is no file, throw exception - s.close(); + } catch (FileNotFoundException exception) { + System.out.println("Error reading the file: " + exception.getMessage()); // if there is no file, throw exception + scanner.close(); return; } - Map>> mp2 = new HashMap<>(); // populating the mp2 hashmap with file content - for (Map d : structureMap) { - String id = d.get("id"); - List> lst = mp2.get(id); + Map>> populatedMap = new HashMap<>(); // populating the mp2 hashmap with file content + for (Map data : structureMap) { + String id = data.get("id"); + List> lst = populatedMap.get(id); if (lst == null) { lst = new ArrayList<>(); - mp2.put(id, lst); + populatedMap.put(id, lst); } - lst.add(d); + lst.add(data); } - int cnt = mp2.size(); + int cnt = populatedMap.size(); System.out.println("There are " + cnt + " forks available (fork1 to fork" + cnt + ")."); System.out.print("Enter the fork number to analyze (or 'all' for all forks): "); - String inp = s.nextLine(); + String inp = scanner.nextLine(); List> sel; if (inp.equalsIgnoreCase("all")) { sel = structureMap; } else { String id = "fork" + inp; - sel = mp2.get(id); + sel = populatedMap.get(id); } int sz = sel.size(); @@ -98,6 +98,6 @@ public static void main(String[] args) { System.out.println("Max lines changed in a commit: " + mx); System.out.println("Min lines changed in a commit: " + mn); - s.close(); + scanner.close(); } } From 5ccffab76ac7aa9e1e375e397ced96c42e6def2b Mon Sep 17 00:00:00 2001 From: Alexander Ruban Date: Tue, 15 Oct 2024 15:04:10 -0700 Subject: [PATCH 3/3] Completed wave 2 by adding the helper method. --- src/Main.java | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Main.java b/src/Main.java index 3ae8312..128a191 100644 --- a/src/Main.java +++ b/src/Main.java @@ -5,14 +5,9 @@ import java.util.*; public class Main { - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); // instantiating new scanner object - - System.out.print("Enter the CSV filename: "); // prompting the user for file input - String fileName = scanner.nextLine(); - + public static List> parseCSV(String filename) { List> structureMap = new ArrayList<>(); // creates a map to structure the CSV columns - try (Scanner fileScanner = new Scanner(new File(fileName))) { + try (Scanner fileScanner = new Scanner(new File(filename))) { fileScanner.nextLine(); while (fileScanner.hasNextLine()) { @@ -28,12 +23,19 @@ public static void main(String[] args) { } } catch (FileNotFoundException exception) { System.out.println("Error reading the file: " + exception.getMessage()); // if there is no file, throw exception - scanner.close(); - return; + // scanner.close(); + // return null; } + return structureMap; + } + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); // instantiating new scanner object + + System.out.print("Enter the CSV filename: "); // prompting the user for file input + String fileName = scanner.nextLine(); Map>> populatedMap = new HashMap<>(); // populating the mp2 hashmap with file content - for (Map data : structureMap) { + for (Map data : parseCSV(fileName)) { String id = data.get("id"); List> lst = populatedMap.get(id); if (lst == null) { @@ -50,7 +52,7 @@ public static void main(String[] args) { List> sel; if (inp.equalsIgnoreCase("all")) { - sel = structureMap; + sel = parseCSV(fileName); } else { String id = "fork" + inp; sel = populatedMap.get(id);