Ok, I got the assignment from my CompSci teacher to write a program that converts a string of an infix operation to a postfix one and it has to use a list and stack. The class that we have to write, Postfix, has to include a conversion, parsing, clear, and reverse methods. I have 3 other classes a ListReferenceBaseCSCE1040
, StackReferenceBaseCSCE1040
, and Node
that he gave us earlier in the year and then a MyTest class that actually gives something to convert and displays the results. I wrote everything down and started coding the Postfix class, and I was sure I had it, until I compiled it, which usually happens, except here I have no idea how to fix whats wrong mainly because out of 3 programs, they all say something different is wrong about the same thing.
I have 2 problems:
1) In lines 5 and 7 where I try to create a list called token and a stack called aStack, it apparently is right even though this is what I was taught.
2) In the parsing method, where it looks at the string at a specified character, it says that I can't use input.charAt(x)
because they are incompatible types. And it also says that when I use character.isDigit(input.charAt(x))
it can't find the variable character. What I was told by the professor is that putting character infront converts it to a basic type.
I'm posting the code below. I would be grateful for any help you guys could give. Thanks
class Postfix {
private ListReferenceBaseCSCE1040 token; //THIS IS WHERE IT GETS SQUIRRLY FOR THE FIRST PART
private String output;
private StackReferenceBaseCSCE1040 aStack;
public Postfix(){
token = new ListReferenceBaseCSCE1040();
aStack = new StackReferenceBaseCSCE1040();
output = "";
}//end constructor postfix
//this takes token and converts it to postfix
public void conversion(){
for(int i = 1; i < token.size(); i++){
if( token.get(i).equals("(")){
aStack.push(token.get(i));
}else if(token.get(i).equals(")")){
while(!aStack.peek().equals("(")){
output += aStack.pop();
} //end while
}else if((token.get(i).equals("*")) || (token.get(i).equals("/")) || (token.get(i).equals("+")) || (token.get(i).equals("-"))){
while(!aStack.isEmpty() && precedence(token.get(i)) <= precedence(aStack.peek())){
output = output + " " + aStack.pop();
} //end while
aStack.push(token.get(i));
}else{
output = output + token.get(i);
} //end ifs
} //end for
while(!aStack.isEmpty()){
output = output + aStack.pop();
} // end while
System.out.println("Conversion of Infix to Postfix");
System.out.println("Input: " + input);
System.out.println("Output: " + output);
} //end conversion
// sets the precedence of the operators
public int precedence(Object op){
if((op.equals("*")) || (op.equals("/"))){
return 2;
}else if((op.equals("+")) || (op.equals("-"))){
return 1;
}else{ //parenthesis
return 0;
} //end ifs
} //end precedence
public void parsing(String input){
String aToken = "";
int numToken = 0;
int i = 0;
while( i < input.length() ){
// THIS IS WHERE IT GET FREAKY FOR THE SECOND PART
if((input.charAt(i) == "(") || (input.charAt(i) == ")") || (input.charAt(i) == "*") || (input.charAt(i) == "/") || (input.charAt(i) == "+") || (input.charAt(i) == "-")){
aToken = aToken + input.charAt(i);
numToken++;
token.add(numToken, aToken);
aToken = "";
}else if(character.isLetter(input.charAt(i))){
aToken += input.charAt(i);
while((i+1<input.length()) && (character.isLetter(input.charAt(i+1))) || (character.isDigit(input.charAt(i+1)))){
i++;
aToken += input.charAt(i);
} //end while
numToken++;
token.add(numToken, aToken);
aToken = "";
}else if(character.isDigit(input.charAt(i))){
aToken += input.charAt(i);
while((i+1 < input.length()) && (character.isDigit(input.charAt(i+1)))){
i++;
aToken += input.charAt(i);
} // end while
numToken++;
token.add(numToken, aToken);
aToken = "";
}else{
//parenthesis
}// end ifs
i++; //adds to the main while loop
} // end while
} // end parsing
public void clear(){
token = new ListReferenceBaseCSCE1040();
aStack = new StackReferenceBaseCSCE1040();
output = "";
}
}//end postfix