I\'ve been assigned to write a class that takes a mathematical expression in infix notation and turns that expression into an equivalent one in postfix notation. That part I
You problem is about scoping of your data field postfix
This is what you have :
public class InfixToPostfix{
private Deque<String> postfix; <-- this is data field
public InfixToPostfix(String infix){
Deque<String> postfix = new LinkedList<String>();
^
|
you declared that reference here again which shadows the data field.
postfix is just visible in constructor and out of here your data field
is still pointing to null value.
change it to
postfix = new LinkedList<String>();
as a result you will instantiate the postfix and when you want to access it , it will never be null
because you instantiated the data field postfix
.
Some Suggestions:
Java 7
For example:
List<String> myList = new ArrayList<String>();
can be written
List<String> myList = new ArrayList< >();
^
|
and if you can choose different name for your iterator function in below code is better because you may confuse whoever reads your code
public Iterator<String> iterator(){
return new PostfixIterator(postfix);
}
It seems that you declare variable "postfix" twice, you should use the class variable "postfix" only.
public class InfixToPostfix{
private Deque<String> postfix;
public InfixToPostfix(String infix){
postfix = new LinkedList<String>();
// Code here
}
}