繁体   English   中英

Java JFrame字符串索引超出范围错误

[英]Java JFrame String Index Out of Bounds Error

我正在Net-beans JFrame中制作计算器,并使用堆栈来帮助计算输入的变量。 我似乎已经遇到了此错误StringIndexOutOfBounds:0,而且我似乎无法弄清楚在发生这种情况时如何解决。 每当我按下启动堆栈的相等按钮时,就会弹出错误。 我认为我的Stack出了点问题,但我仍然无法弄清楚。 我真的需要对此有一些新的了解。

我使用/导入了秋千和.awts,但我不认为它们给我带来的错误是我的秋千。

    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;

这是我的堆栈:

   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();
}

}

这是我认为给我这个错误的代码

   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();  
       }   
    }

任何帮助将非常感激。 我真的很迷失在这里。 谢谢。

问题来了:

token = exp.substring(0,loc);

上一行从exp获取一个子字符串。 稍后,您执行以下操作:

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

发生的是:从exp剪切并存储到令牌中的字符串为 因此,当您尝试访问该字符串的索引0时,系统会告诉您:该字符串甚至没有索引0(并且只有在token空时才会发生!)。

因此,这里的答案有两个:

  1. 在进行诸如charAt(someIndex)之类的调用之前,最好先检查一下字符串是否确实具有该索引
  2. 但就您而言,仅进行检查无济于事。

您会看到,您的问题基本上是您计算“子字符串”索引的逻辑可能是错误的。

我的建议是:坐下,然后手动处理输入数据。 含义:使用示例输入,并手动执行程序。 要了解您的计数器/索引变量的外观,请了解您的代码对其输入的实际作用。 如果您觉得这太麻烦了,那么至少要学会使用调试器来做到这一点(但是,一次手动完成两次,仍然会告诉您50或100个以上的答案!)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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