0
import java.io.*;

public class runner{
//Translates from infix to postfix
//Evaluates the postfix expression

public static tokenlist xform(String input){
    //operand get pushed automatically onto linked list
    //operator we will see;
    tokenlist postfix=new tokenlist();
    stack stack=new stack();
    int c=0;
    while(input.substring(c,c) != null){
        if(prec.isoper(input.substring(c,c))==false){
            postfix.push(input.substring(c,c),false);
        }else{
            if(stack.inspect()==null){
                stack.push(input.substring(c,c));
            }else{
                if(prec.inprec(input.substring(c,c))>prec.stackprec(stack.inspect())){
                    while(prec.inprec(input.substring(c,c))>prec.stackprec(stack.inspect())){
                        String s=stack.pop();
                        postfix.push(s,true);
                    }
                }else{
                    stack.push(input.substring(c,c));
                }
            }
        }
        c++;
    }
    return postfix;
}

public static double eval(tokenlist postfix){
    astack numbers=new astack();
    Double ans=0.0;
    numbers.push(Double.parseDouble(postfix.getTing()));
    while(numbers.isEmpty() != true){
        if(postfix.getFunc()== false){
            numbers.push(Double.parseDouble(postfix.getTing()));
        }else{
            double c=0.0;
            double a=numbers.pop();
            double b=numbers.pop();
            if(postfix.getTing()=="+"){
                c=a+b;
                numbers.push(c);
            }
            if(postfix.getTing()=="-"){
                c=b-a;
                numbers.push(c);
            }
            if(postfix.getTing()=="*"){
                c=a*b;
                numbers.push(c);
            }
            if(postfix.getTing()=="/"){
                c=b/a;
                numbers.push(c);
            }
            if(postfix.getTing()=="^"){
                double store;
                double rep=0.0;
                while(rep<=a){
                    c=b*b;
                    store=c;
                    rep=rep+1.0;
                }
                numbers.push(c);
            }
            if(postfix.getTing()=="\\"){
                c=Math.abs(b-a);
                numbers.push(c);
            }
        }
        ans=numbers.pop();
    }
    return ans;
}

public static void main(String[] args){
    try{
        tokenlist postfix=xform(args[0]);
        postfix.printlist();
        double d=eval(postfix);
        System.out.println(d);
    }catch(Exception e){System.out.print(e);}
}

}

Regardless of what number is within args[#] in the main, I keep geting the above exception and String index out of range: Whatever number is in args[#] in the main. I already checked in the xform and it takes in the input as a string and the substring starts at 0. Still it doesn't work. I f anyone could have an explanation or tips they would be greatly appreciated.

Haukka
  • 27
  • 6
  • Where? Have you tried debugging it? – luk2302 Dec 22 '15 at 22:45
  • 1
    There should be a stack trace associated with the exception. It will tell you from exactly which source line the exception was thrown. – John Bollinger Dec 22 '15 at 22:45
  • In any case, a `StringIndexOutOfBoundsException` says you are trying to access characters of a string by index, and the index you are using is either negative or larger than the length of the `String`. The exception's detail message probably even tells you what the erroneous index is. – John Bollinger Dec 22 '15 at 22:47
  • 2
    If you change the conditions of your while loop to `while (c < input.length())` that should fix your issue – Taelsin Dec 22 '15 at 22:49
  • You seem to have misunderstood how [substring](http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#substring-int-int-) works. – Klitos Kyriacou Dec 22 '15 at 22:49
  • Also, read http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – JB Nizet Dec 22 '15 at 22:49
  • @KlitosKyriacou He's using it just fine as long as he only wants one character. – Taelsin Dec 22 '15 at 22:50
  • 1
    @Taelsin No, he's getting an empty string each time because the 'end' argument is meant to be exclusive. – Klitos Kyriacou Dec 22 '15 at 22:51
  • @KlitosKyriacou you're right. easy fix though. – Taelsin Dec 22 '15 at 22:53
  • I did misunderstand substring. Taelsin wwas right. That did ix my problem – Haukka Dec 24 '15 at 13:32

1 Answers1

1

You're code contains the line while(input.substring(c,c) != null)
This is basically an endless loop until c is higher that the length of your string which causes the exception.
Try to change it to while(c < input.length()) and you should be good to go.

Furthermore I hope you are aware of the fact that substring(c,c) will always return an empty string.
If you want a single character either use .charAt(c) or .substring(c, c+1)

Raven
  • 2,951
  • 2
  • 26
  • 42
  • Thanks. That fixed that problem but now I get a null pointer exception. I changed them all to substring(c,c+1) like you said. I did java runner "0*2", is that how it's supposed to be inputted. Any ideas? – Haukka Dec 22 '15 at 23:53
  • where do you get a `nullPointerException`? – Raven Dec 23 '15 at 15:54
  • I get it in the if statement while loop combination. if(prec.inprec(input.substring(c,c))>prec.stackprec(stack.inspect())){ while(prec.inprec(input.substring(c,c))>prec.stackprec(stack.inspect()) && stack.isEmpty()== false){ String s=stack.pop(); postfix.push(s,true); } – Haukka Dec 24 '15 at 13:30
  • As far as I can see it either the `prec` or `postfix` varibale seems to be `null` somehow – Raven Dec 24 '15 at 13:41
  • Yeah. I used System.out.println to output the results of prec.inprec and prec.stackprec, both of which were the correct values so that I know that that isn't a problem. postfix is a linkedlist, and it works as long as I input something that avoids the if statement while loop combo. I have also tried getting rid of the if statement, just leaving the while loop, and changing the conditions in the while loop to stop when either, a) that stack is empty, or b) when prec.inprec – Haukka Dec 24 '15 at 15:49
  • prints out test statements if you want me to add that, I think it might have to do with exiting the while loop. – Haukka Dec 24 '15 at 15:49
  • I would suggest you to use eclipse and the integrated debugger to locate the problem... – Raven Dec 24 '15 at 16:43
  • Ok. Thank You very much – Haukka Dec 24 '15 at 19:01