简体   繁体   中英

Java JFrame String Index Out of Bounds Error

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.

The problem comes here:

token = exp.substring(0,loc);

The above line takes a substring from exp. A bit later you do:

char ch = token.charAt(0);//there is a specific problem with this line

And what happens is: the string that you cut from exp and store into token ... is empty . And therefore, when you try to access index 0 of that string you are told: that string doesn't even have an index 0 (and that can only happen if token is empty !).

Thus the answer here is two-fold:

  1. Before doing calls like charAt(someIndex) you better check upfront if your string actually has that index
  2. But in your case, checking alone wouldn't help.

You see, your problem is basically that your logic how you compute your "substring" index is probably wrong.

My suggestion: sit down, and process your input data manually. Meaning: use example input, and manually execute your program. To understand how your counters/index variables look like, to understand what your code is really doing with its input. If you find that too cumbersome, than at least learn using a debugger to do that (but doing it one, two times manually will still tell you more than 50 or 100 answers here on SO!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM