So I think I am close; however, I am getting some weird output when I print the contents of my array. So the input file is just a text file not an actual csv file but here is what the contents of the input file look like:
A,B,C,D,E,F
∞,1,∞,∞,∞,∞
∞,5,∞,1,11,∞
∞,∞,∞,∞,∞,∞
5,∞,∞,∞,3,∞
∞,∞,∞,11,∞,7
∞,∞,3,∞,∞,∞
Pay no attention to case 2 and 3. Also, I haven't actually built any of the algorithms I just want to build them from the contents of my 2D array but first i need to make sure those contents are correct. So here is my code:
public class GraphAlgorithms { public static void main(String[] args) throws IOException { GraphAlgorithms newAlgorithm = new GraphAlgorithms(); String inputFile = "src/input/input.txt"; String[][] readCSV = newAlgorithm.readCSV(inputFile); Scanner sc = new Scanner(System.in); System.out.println("Please select the algorithm you wish to run: " + "1 for Prim's algorithm, 2 for Kruskal's algorithm" + "or 3 for Floyd-Warshall’s algorithm "); while (sc.hasNext()){ int i = sc.nextInt(); switch(i){ case 1: System.out.println("You chose Prim's algorithm."); primsAlgorithm(readCSV); break; case 2: System.out.println("You chose Kruskal's algorithm."); break; case 3: System.out.println("You chose Floyd-Warshall's algorithm."); break; default: System.out.println("Invalid entry"); break; } System.out.println(); System.out.println("Please select the algorithm you wish to run: " + "1 for Prim's algorithm, 2 for Kruskal's algorithm" + "or 3 for Floyd-Warshall’s algorithm "); } } public static String[][] readCSV(String path) throws FileNotFoundException, IOException { String fName = path; String thisLine; FileInputStream fis = new FileInputStream(fName); DataInputStream myInput = new DataInputStream(fis); List<String[]> lines = new ArrayList<>(); while ((thisLine = myInput.readLine()) != null) { lines.add(thisLine.split(",")); } // convert our list to a String array. String[][] array = new String[lines.size()][0]; return lines.toArray(array); } public static void primsAlgorithm (String [][] array){ for (String[] strings : array) { for (String string : strings) { System.out.print("\t" + string); } System.out.println(); } } }
So the odd part is this is what is being printed when I select 1 as my first option in order to print the array contents:
output.jpg
So the only part of the output that is not expected for me is the two characters printed prior to A. Why is it printing those?


LinkBack URL
About LinkBacks
Reply With Quote