diff --git a/src/main/java/NumberTriangle.java b/src/main/java/NumberTriangle.java index 30e10ecb..d5a61b43 100644 --- a/src/main/java/NumberTriangle.java +++ b/src/main/java/NumberTriangle.java @@ -1,4 +1,5 @@ import java.io.*; +import java.util.*; /** * This is the provided NumberTriangle class to be used in this coding task. @@ -104,31 +105,39 @@ public int retrieve(String path) { * @throws IOException may naturally occur if an issue reading the file occurs */ public static NumberTriangle loadTriangle(String fname) throws IOException { - // open the file and get a BufferedReader object whose methods - // are more convenient to work with when reading the file contents. InputStream inputStream = NumberTriangle.class.getClassLoader().getResourceAsStream(fname); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); + // List to hold each level of the triangle + List> levels = new ArrayList<>(); - // TODO define any variables that you want to use to store things + String line; + while ((line = br.readLine()) != null) { + line = line.trim(); + if (line.isEmpty()) continue; - // will need to return the top of the NumberTriangle, - // so might want a variable for that. - NumberTriangle top = null; - - String line = br.readLine(); - while (line != null) { - - // remove when done; this line is included so running starter code prints the contents of the file - System.out.println(line); + String[] nums = line.split(" "); + List currentLevel = new ArrayList<>(); + for (String n : nums) { + currentLevel.add(new NumberTriangle(Integer.parseInt(n))); + } + levels.add(currentLevel); + } + br.close(); - // TODO process the line + // Connect parent nodes to children + for (int i = 0; i < levels.size() - 1; i++) { + List current = levels.get(i); + List next = levels.get(i + 1); - //read the next line - line = br.readLine(); + for (int j = 0; j < current.size(); j++) { + current.get(j).setLeft(next.get(j)); + current.get(j).setRight(next.get(j + 1)); + } } - br.close(); - return top; + + // The topmost node + return levels.get(0).get(0); } public static void main(String[] args) throws IOException {