[英]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. 我正在Net-beans JFrame中制作计算器,并使用堆栈来帮助计算输入的变量。 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.
我似乎已经遇到了此错误StringIndexOutOfBounds:0,而且我似乎无法弄清楚在发生这种情况时如何解决。 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.
我认为我的Stack出了点问题,但我仍然无法弄清楚。 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. 我使用/导入了秋千和.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;
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. 上一行从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 . 发生的是:从exp剪切并存储到令牌中的字符串为空 。 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 !).
因此,当您尝试访问该字符串的索引0时,系统会告诉您:该字符串甚至没有索引0(并且只有在token为空时才会发生!)。
Thus the answer here is two-fold: 因此,这里的答案有两个:
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!)
如果您觉得这太麻烦了,那么至少要学会使用调试器来做到这一点(但是,一次手动完成两次,仍然会告诉您50或100个以上的答案!)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.