我在Net-beans JFrame中製作一個計算器,並使用堆棧來幫助計算輸入的變量。我似乎遇到了這個錯誤StringIndexOutOfBounds:0,我似乎無法弄清楚如何解決它發生時。每當我按下啓動堆棧的相等按鈕,彈出錯誤。我認爲它的錯誤與我的堆棧,但我再也找不出來。我真的需要一些新鮮的眼光。Java JFrame字符串索引超出界限錯誤
我使用/進口的鞦韆和.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();
}
}
任何幫助將是非常讚賞的代碼。我好像真的迷失在這裏。謝謝。
加入整個堆棧跟蹤到你的帖子。 –