Note that there are some explanatory texts on larger screens.

plurals
  1. POI'm having trouble with the Node data structure -- trying to make it more efficient with loops
    primarykey
    data
    text
    <p>So I'm having a bit of an issue with my code. My objective is to take an adjacency matrix from a file and input it into a 2d array. I was able to do that. Now I'm using the data structure for Breadth First Search and Nodes to work with that array.</p> <p>Right now, I'm trying to simply create new Nodes, but I cannot make nodes due to being a char. Here let me post my full code. I'll post the error below.</p> <hr> <pre><code>package javaapplication1; import java.io.*; import java.util.*; import tio.*; import java.lang.*; public class JavaApplication1 { private static int row = 0; private static int col = 0; private static int n = 20; private static int[][]adjMatrix = new int[n][n]; public static int[][] adjMatrix() throws FileNotFoundException, IOException{ //int n = 20; //int row = 0; //int col = 0; //int[][]adjMatrix = new int[n][n]; String file = ("C:\\Users\\David\\Documents\\NetBeansProjects\\JavaApplication1\\src\\javaapplication1\\adjmatrix.txt"); BufferedReader in = new BufferedReader(new FileReader(file)); String line; //System.out.println(in.readLine()); int k = 0; while ((line = in.readLine()) != null){ //System.out.println(row); String[] temp = line.split("\\s+"); for(col = 0; col &lt; adjMatrix[row].length; col++){ adjMatrix[row][col] = Integer.parseInt(temp[col]); // System.out.println(" " + temp[col] + " " + col); } row++; } //end while //System.out.print(array[4][1]); in.close(); return adjMatrix; } // endclass public static void main(String[] args) throws IOException{ adjMatrix(); // Create the nodes (20 based off adj matrix given Node nA =new Node('1'); Node nB =new Node('2'); Node nC = new Node('3'); Node nD = new Node('4'); Node nE = new Node('5'); Node nF=new Node('6'); Node nG=new Node('7'); Node nH=new Node('8'); Node nI=new Node('9'); Node nJ=new Node('10'); Node nK=new Node('11'); Node nL=new Node('12'); Node nM=new Node('13'); Node nN=new Node('14'); Node nO=new Node('15'); Node nP=new Node('16'); Node nQ=new Node('17'); Node nR=new Node('18'); Node nS=new Node('19'); Node nT=new Node('20'); // Create a graph, adding the nodes, and creating edges Graph g = new Graph(); for (int i=1;i&lt;=20;i++){ String aString = Integer.toString(i); aString = n+aString; g.addNode(aString); } // g.addNode(nA); // g.addNode(nB); // g.addNode(nC); // g.addNode(nD); // g.addNode(nE); // g.addNode(nF); // g.addNode(nG); // g.addNode(nH); // g.addNode(nI); // g.addNode(nJ); // g.addNode(nK); // g.addNode(nL); // g.addNode(nM); // g.addNode(nN); // g.addNode(nO); // g.addNode(nP); // g.addNode(nQ); // g.addNode(nR); // g.addNode(nS); // g.addNode(nT); // g.addNode(nU); // g.setRootNode(nA); // g.connectNode(nA,nB); // g.connectNode(nA,nD); // g.connectNode(nA,nE); // g.connectNode(nA,nF); // g.connectNode(nA,nG); // // g.connectNode(nB,nE); // g.connectNode(nB,nF); // g.connectNode(nB,nG); // // g.connectNode(nC, nD); // g.connectNode(nC,nE); // g.connectNode(nC,nF); // g.connectNode(nC,nG); // // g.connectNode(nD,nE); // g.connectNode(nD,nF); // // g.connectNode(nE, nF); // g.connectNode(nE,nG); // // g.connectNode(nF,nG); // // g.connectNode(nH, nI); // g.connectNode(nH,nJ); // g.connectNode(nH,nK); // g.connectNode(nI,nJ); // g.connectNode(nI,nK); // g.connectNode(nI,nL); g.bfs(); } // end main } // end class </code></pre> <p>I know that is a lot of code, but that is the reason why I would really like to not brute force the g.connectNode and the g.addNode. Anyways I can implement a loop for this? Also, when I start doing "Node nJ=new Node('10');" it gives me errors since my Node is a char, any suggestions on bypassing that without breaking everything? The error is an unclosed character literal.</p> <p>Here is the code for node.</p> <pre><code>public class Node { public char label; public boolean visited=false; public Node(char l) { this.label=l; } } </code></pre> <p>Thank you for your help, and let me know if I need to edit or add anything.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload