I am making a calculator in Net-beans JFrame and using a Stack to help calculated the variables that are inputted. I seem to have run in to this error StringIndexOutOfBounds: 0 and i can't seem to figure out how to solve it when it happens. Whenever I press the equal button that initiates the Stack the error pops up. I think its something wrong with my Stack but again I can't figure it out. And I really need some fresh eyes on this.
I used/imported swings and .awts but I don't think they are giving me the error here are my swings.
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.Math.round;
import java.util.NoSuchElementException;
import java.util.Scanner;
import javax.swing.JFileChooser;
Here is my Stack:
public class StackCalc
{
private LinkedList stackList = new LinkedList();
private int top, maxTop;
public Object removedEle;
public Object topEle;
public StackCalc(int mt)
{
maxTop=mt;
top=-1;
}
public boolean isFull()
{
return top == maxTop-1;
}
public boolean push (Object O)
{
if(!isFull())
{
stackList.addFirst(O);
top++;
return true;
}
else
{
return false;
}
}
public boolean pop()
{
if(!stackList.isEmpty())
{
removedEle= stackList.removeFirst();
top--;
return true;
}
else
{
return false;
}
}
public void getTop()
{
topEle=stackList.getFirst();
}
public boolean isEmpty()
{
return stackList.isEmpty();
}
}
Here is the code that I think is giving me this error
static void processExpR(String exp)
{
boolean advance = true;
String token = " ";
int loc = exp.indexOf(token);
while (loc != -1)
{
if (token.isEmpty()){
return;
}
else if (advance){
token = exp.substring(0,loc);
exp = exp.substring(loc+1);
}
char ch = token.charAt(0);//there is a specific problem with this line
if(Character.isDigit(ch)){
advance = true;
s1R.push(token);
}
else
{
if(s2R.isEmpty())
{
advance = true;
s2R.push(token);
}
else
{
advance = false;
calcR();
}
}
if(advance){
loc = exp.indexOf(" ");
}
}//end of while
if (Character.isDigit(exp.charAt(0)))
{
s1R.push(exp);
}
else
{
s2R.push(exp);
}
while (!s2R.isEmpty())
{
calcR();
}
}
Any help would be much appreciated. I'm like really lost here. Thank you.