Title: How to convert objects in a LinkedList to a String?
Tags: java
Question: I am making a Graph class that needs a method for Breadth First Search and another for printing the Adjacency List Matrix. My Vertex class uses &quot;int&quot; to make a new Vertex so I've had to go back and forth between Vertex and int, which is where I ran into this problem. I need some way to convert the Vertex objects in the LinkedList to a String for printing, and in some cases to be converted back into an int (I am using the Scanner nextInt() method with the String but because of how the LinkedList is storing elements that throws an InputMismatchException). Aopologies for the long post but I wasn't sure what all would be needed for help so I copied both methods.
public static void breadthFirstSearch(Graph G, int root) //Initializes a search of the graph using the graph and a vertex to start from (default: 0)
Vertex rootVertex = new Vertex(root); //Creates a reference to the original root vertex to be used to make a &quot;cloud of vertices&quot;
Queue<Vertex&gt; queue = new LinkedList<Vertex&gt;(); //Makes a Queue that will hold the visited vertices
queue.add(rootVertex); //Adds the root vertex to the Queue
visitedNodes.add(rootVertex); //Adds the root vertex to the list of visited vertices since it has been visited
while(!queue.isEmpty()) //This loop will be executed until the Queue is empty
{
String listHead = &quot;&quot; + queue.poll() + &quot;&quot;;
System.out.println(listHead);
Scanner scan = new Scanner(listHead);
int vertex = scan.nextInt();
for(Vertex v : Vertex.getAdjacentVertices(vertex)) //&quot;For every vertex in the current vertex's list of adjacent vertices&quot; this loop will execute
{
if(!visitedNodes.contains(v)) //If the vertex is not in the list of visited nodes, now it is!
{
visitedNodes.add(v);
queue.add(v);
}
}
}
String BFSorder = &quot;&quot;; //Initializes an empty String that will hold the list of visited vertices in the order in which they were visited
for(int i = 0; i < visitedNodes.size(); i++) //Iterates through the list of visitedNodes
{
String currNode = &quot;&quot; + visitedNodes.get(i) + &quot; &quot;; //Transforms the current vertex into a String so it can be added to BFSorder
BFSorder = BFSorder + currNode; //Adds the String representing the current Node to BFSorder
}
System.out.println(&quot;Graph nodes visited in BFS Order: &quot; + BFSorder + &quot;&quot;); //Prints out the list of visited vertices
public static void print(Graph G) //This method prints out an Adjacency List Matrix using the parameter Graph
for(int i = 0; i < visitedNodes.size(); i++) //Iterates through the list of visited nodes
{
String adjVertex = &quot;Adjacency list for vertex &quot; + visitedNodes.get(i) + &quot; Head &quot;; //For each node, a new String is made
String currVertex = &quot;&quot; + visitedNodes.get(i) + &quot;&quot;; //These next three lines convert the vertex to an integer so it can be used for the for loop
Scanner scan = new Scanner(currVertex);
int vertex = scan.nextInt();
for(Vertex v : Vertex.getAdjacentVertices(vertex)) //Goes through the list of vertices adjacent to the current Vertex
{
String currNode = &quot; -&gt; &quot; + v + &quot;&quot;; //Makes the vertex into a String with special formatting
adjVertex = adjVertex + currNode; //Adds the new string to adjVertex
}
System.out.println(adjVertex); //Prints out the list of adjacent vertices for the current vertex
}
Thanks a bunch!
Comment: For printing objects to the screen, just override the toString() method and customize it how you want.

Comment: @Femke Thanks, that worked, now I am running into other errors. Progress...?



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:12/09/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java>', 'Title': 'How to convert objects in a LinkedList to a String?', 'ViewCount': 31, 'id': 70284673, 'user_AboutMe': None, 'user_AccountId': 23113921.0, 'user_DisplayName': 'Sam', 'user_Reputation': 1.0, 'user_Views': 1.0, 'user_id': 17221486.0}}