I'm trying to implement Shunting Yard algorithm. When I run this program I somehow receive NullPointerException in one line(comment that one). I've set an if statement there but seems that doesn't do anything. I've also tried to compare 2 strings like If(!string.equalsIgnoreCase("(")) but it didn't work too. Why is that so? What is wrong with my code?
/**
* Main class of the Java program.
*/
import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;
public class Main{
public static LinkedStack<String> stackForNumbers;
public static LinkedStack<String> stackForOperators;
public static Map<String, Integer> operatorPrecedance = new HashMap<String, Integer>();
public static void main(String[] args) {
String str = "1+2-3*(4+5)";
stackForNumbers = new LinkedStack<>();
stackForOperators = new LinkedStack<>();
int b = 0;
int e = -1;
for(int i = 1; i<=str.length(); i++){
if(!Character.isDigit(str.charAt(i-1))){
b = e + 1;
e = i-1;
String s = str.substring(b,e);
if(!s.equalsIgnoreCase("")){
stackForNumbers.push(s);
}
String string = String.valueOf(str.charAt(i-1));
System.out.println("String is " + operatorPrecedance.containsKey(string));
if(operatorPrecedance.containsKey(string)){ // This should prevent further if but it does not
if(!stackForOperators.isEmpty() && isPrecedanceHigher(string, stackForOperators.peek())){ //Here is NullPointer Excpetion is detected.
String item = stackForOperators.pop();
stackForOperators.push(string);
stackForOperators.push(item);
}else{
stackForOperators.push(string);
}
}else if(str.charAt(i-1)=='('){
stackForOperators.push(string);
}else if(str.charAt(i-1)==')'){
System.out.println("Position is " + (i-1) + " Char is " + str.charAt(i-1));
while(!stackForOperators.peek().equalsIgnoreCase("(")){
System.out.println("h");
calculation((double)0, (double)0);
}
}
}
}
/*for(String s:stackForOperators){
System.out.println("Operator in stackForOperators is " + s);
}
for(String s:stackForNumbers){
System.out.println("Number in stackForNumbers is " + s);
}*/
}
public static void calculation(Double i, Double j){
for(String s:stackForOperators){
System.out.println("Operator in stackForOperators is " + s);
}
for(String s:stackForNumbers){
System.out.println("Number in stackForNumbers is " + s);
}
i = Double.parseDouble(stackForNumbers.pop());
j = Double.parseDouble(stackForNumbers.pop());
if(stackForOperators.peek().equalsIgnoreCase("+")){
i = j + i;
}else if(stackForOperators.peek().equalsIgnoreCase("-")){
i = j - i;
}else if(stackForOperators.peek().equalsIgnoreCase("*")){
i = j * i;
}else if(stackForOperators.peek().equalsIgnoreCase("/")){
i = j / i;
}
stackForOperators.pop();
stackForNumbers.push("" + i);
}
public static boolean isPrecedanceHigher(String a, String b){
System.out.println(b + " " + operatorPrecedance.get(b));
System.out.println(a + " " + operatorPrecedance.get(a));
return (operatorPrecedance.containsKey(a) && operatorPrecedance.get(b)>=operatorPrecedance.get(a));
}
}
LinkedStack class:
import java.util.Iterator;
import java.util.NoSuchElementException;
public class LinkedStack<Item> implements Iterable<Item>{
Node original;
int n;
public LinkedStack(){
original = null;
n = 0;
}
public void push(Item item){
Node node = original;
original = new Node();
original.item = item;
original.node = node;
n++;
}
public boolean isEmpty(){
return n == 0;
}
public Item peek(){
if(original==null){
throw new NullPointerException();
}
return original.item;
}
public Item pop(){
Item item = original.item;
original = original.node;
n--;
return item;
}
public int size(){
return n;
}
private class Node{
private Node node;
private Item item;
}
public Iterator<Item> iterator(){
return new myIterator();
}
private class myIterator implements Iterator<Item>{
private Node node = original;
public boolean hasNext(){return node != null;}
public void remove(){throw new UnsupportedOperationException();}
public Item next(){
if(!hasNext()){
throw new NoSuchElementException();
}
Item item = node.item;
node = node.node;
return item;
}
}
}
Aucun commentaire:
Enregistrer un commentaire