i am working on an infix to postfix project and have reached a troubling point.
one by one i must extract a string token from the string being inputted to the method infixtopostfix and one by one input them to the front of the deque named postfix.
i dont understand what is wrong with what i am doing as i am moving through my while loop pulling one token at a time from the string using
while(infixString.hasMoreTokens() )
{
String temp = infixString.nextToken();
and inserting one by one into my deque string named postfix using:
postfix.addFirst(temp);
i suppose i am dealing with an obvious syntax error or small logical error. i am receiving the following error:
Exception in thread "main" java.lang.NullPointerException
at InfixToPostfix.<init>(InfixToPostfix.java:22)
at InfixToPostfix.main(InfixToPostfix.java:58)
please if someone could shed some light in the right direction i would very much appreciate it and this is my code thus far:
import java.util.*;
import java.util.Deque;
import java.util.StringTokenizer; //imports class to tokenize a string
/** Class documentation goes here. Write more than you did last time.
*/
public class InfixToPostfix {
private Deque<String> postfix; // Used as a queue of String
/** Method documentation goes here
*/
public InfixToPostfix(String infix) {//parse through string and turn into tokens.
StringTokenizer infixString = new StringTokenizer(infix, "+-/*()", true);
while(infixString.hasMoreTokens() )
{
String temp = infixString.nextToken();
postfix.addFirst(temp);
System.out.println(infixString.nextToken());
/*
if(# or variable){
move to outputqueue
}
if()
if()*/
}
//output oken when parsing..........we get:
//on left prefix notation
//above infix notation
//on right postfix notation
// Build postfix as a queue representing a postfix expression
postfix = new LinkedList<String>();
}
/** Mehtod documentation goes here
*/
public Iterator<String> iterator() {
return new PostfixIterator(postfix) ;
}
public static void main(String[] args) {
// Simple embedded unit-test for InfixToPostfix
InfixToPostfix test = new InfixToPostfix("2+3*5");
}
}
// PostfixIterator implements Iterator<String>, so is an acceptable type
// whenever Iterator<String> is specified as the type (eg, iterator() above).
class PostfixIterator implements Iterator<String> {
private Deque<String> postfix;
public PostfixIterator(Deque<String> postfix) {
this.postfix = postfix;
}
public boolean hasNext() {
return true;
}
public String next() {
return "";
}
}