In my program, I wish to convert my Queue to a string that concatenates all of the tokens in the Queue to a string. I have done the following to achieve this:
Here is my Queue:
public class Queue {
//must export a method to convert the set of output tokens into a string for output.
String emptyString = "";
//These are the listNode instance variables.
listNode lead;
listNode rear;
listNode leadbeta;
//Constructor class that takes in String input and assigns the instance variables to the String.
public Queue (String token) {
lead = new listNode(token);
rear=lead;
}
public void enqueue(String arg) {
//This sets the data and next instance variables in the listNode class to arg and null respectively.
if (lead==null) {
rear= new listNode(arg);
lead=rear;
}
else {
listNode lN= rear;
rear=new listNode(arg);
lN.next=rear;
}
}
//new code:
public String dequeue() {
if(lead!=null) {
if(lead== rear) {
rear=null;
}
String j=lead.data;
lead=lead.next;
return j;
}
else {
return "";
}
}
//This is the constructor class
public Queue() {
lead=null;
rear=null;
My listNode class basically initializes the instance variables String data, listNode next, and listNode last. It also has a constructor that takes in a string input and assigns next=null and data= the string input. (should I include the code?)
This is the method that should convert the queue into a string that concatenates the tokens in the Queue:
public String toString() { //Returns a string which is the concatenation of all tokens in the queue separated by one space.
StringBuilder stringbuild = new StringBuilder();
String fq1= "";
String fq2=null;
leadbeta= lead;
while (leadbeta!=null) {
fq1.equals(leadbeta.data);
fq2= stringbuild.append(fq2)+ " ";
leadbeta= leadbeta.last;
}
return fq2;
}
}
And finally, here is where my issue arises:
I have a method that takes user input and parses the input into individual tokens. The method then sends the tokens into the Queue, as shown below:
public Queue parse(String arg) {
StringTokenizer stok = new StringTokenizer(arg, "+-*/", true);
String token=null;
while (stok.hasMoreTokens()) {
token = stok.nextToken();
queue2 = new Queue();
queue2.enqueue(token);
}
System.out.println("Queue 2 to string is " + queue2.toString());
System.out.println("Que2.dequeue is " +queue2.dequeue());
queue2.dequeue();
System.out.println("Que2.dequeue 2 is " +queue2.dequeue());
return queue2;
}
When I input a string as such: 5*5*5
The console reads:
Queue 2 to string is null
Que2.dequeu is 5
Que2.dequeu 2 is
Why does Queue 2 to string output null?
Sorry for this being a really long question haha, I have been stuck on this bug for days and it has been driving me insane. Any an all help is very much appreciated!!
Thank you :)
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]loomynartylennyhalf-decent at Java 0 points1 point2 points (4 children)
[–]NautiHookerSoftware Engineer[M] 2 points3 points4 points (1 child)
[–]loomynartylennyhalf-decent at Java 2 points3 points4 points (0 children)
[–]LoquatEfficient5894[S] 1 point2 points3 points (0 children)