[英]How to fix this Run time error?
I am trying to make a java program which will find and remove repetative characters in string. 我正在尝试创建一个java程序,它将查找并删除字符串中的重复字符。 Like if user types "baloon", the output should be "balon".
就像用户输入“baloon”一样,输出应该是“balon”。 I used String variable for input and copied its content to Char array so that I can analyze each character.
我使用String变量作为输入并将其内容复制到Char数组,以便我可以分析每个字符。 I get ArrayIndexOutOfBoundsException.
我得到ArrayIndexOutOfBoundsException。 Here is my code
这是我的代码
class doubleKiller{
private String inputStr = " ";
private char[] catchStr = new char[inputStr.length()];
private String modifiedStr;
//Accessor method
public void getString(String inputStr)
{
this.inputStr = inputStr;
}
public String killRepeater()
{
//copying string data to char array
this.inputStr.getChars(0 , this.inputStr.length() , catchStr , 0);
//------------------
for(int counter = 0 ; counter < this.inputStr.length() ; counter++)
{
if(catchStr[counter] != catchStr[counter - 1])
{
modifiedStr = modifiedStr + catchStr[counter];
}
}
return modifiedStr;
}
} }
Here is Output when killRepeater() is called. 这是调用killRepeater()时的输出。
java.lang.ArrayIndexOutOfBoundsException: src.length=5 srcPos=0 dst.length=1 dstPos=0 length=4
at java.lang.System.arraycopy(Native Method)
at java.lang.String.getChars(String.java:894)
at doubleKiller.killRepeater(Main.java:23)
at useThings.main(Main.java:49)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.aide.ui.build.java.RunJavaActivity$1.run(SourceFile:108)
at java.lang.Thread.run(Thread.java:862)
Tell me my error...Thanks in advance 告诉我我的错误...提前谢谢
You are doing 你在做
private String inputStr = " ";
private char[] catchStr = new char[inputStr.length()];
inputStr.length()
returns 1
, that is, the size from the string " ", so, you have a char array with size 1 . inputStr.length()
返回1
,即字符串 “” 的大小 ,因此,您有一个大小为1的char数组 。
When you do: 当你这样做时:
for(int counter = 0 ; counter < this.inputStr.length() ; counter++){
if(catchStr[counter] != catchStr[counter - 1]){
With a inputStr.length() > 1
you have an ArrayIndexOutOfBoundsException
. 如果
inputStr.length() > 1
则会出现ArrayIndexOutOfBoundsException
。
Also, when run catchStr[counter - 1])
for the first time, counter-1
would generate -1
, which isn't a valid index . 此外,当第一次运行
catchStr[counter - 1])
时, counter-1
将生成-1
, 这不是有效的索引 。 Try running the counter
from 1
to avoid this error. 尝试从
1
运行counter
以避免此错误。
Declare your array with a fixed size (eg 200) or use an ArrayList
. 使用固定大小 (例如200)声明您的数组或使用
ArrayList
。 You could also put catchStr = new char[inputStr.length()]
after taking the input of the String. 您也可以在获取String的输入后放置
catchStr = new char[inputStr.length()]
。
Good thing that you asking about specific problem, but in case you interested how to make this simpler, there is regex magic 你询问具体问题的好事,但如果你有兴趣如何使这更简单,有正则表达式魔法
String in = "ballooooon";
String out = in.replaceAll("(.)\\1+", "$1"); // balon
Which means 意思是
find any symbol, save it to group 1, if next symbol, or symbols are equal to group 1, delete them and leave just one from the group.
找到任何符号,将其保存到组1,如果下一个符号或符号等于组1,则删除它们并从组中只留下一个符号。
You are initializing catchStr with inputStr.length(). 您正在使用inputStr.length()初始化catchStr。 But at this time inputStr = " ".
但此时inputStr =“”。 So catchStr has a length of 1. When you later populate inputStr to a bigger value, you try and copy that into a char[] of length 1 and it doesn't fit.
因此,catchStr的长度为1.当您稍后将inputStr填充到更大的值时,您尝试将其复制到长度为1的char []中,并且它不适合。 Add
catchStr = new char[inputStr.length()];
添加
catchStr = new char[inputStr.length()];
right before you do the copy. 就在你复制之前。
When you initialise catchStr
it will always have the value of 1, because inputStr
is " "
and therefore it's length is 1. 初始化
catchStr
它的值始终为1,因为inputStr
为" "
,因此它的长度为1。
private String inputStr = " ";
private char[] catchStr = new char[inputStr.length()];
When you loop on counter
in the method killRepeater()
you will get an ArrayOutOfBoundsException
because the string you pass in getString
is probably greater than 1. 当您在方法
killRepeater()
循环counter
,您将得到一个ArrayOutOfBoundsException
因为您在getString
传递的字符串可能大于1。
Consider moving new char[inputStr.length()]
into getString
. 考虑将
new char[inputStr.length()]
到getString
。
public void getString(String inputStr) {
this.inputStr = inputStr;
this.catchStr = new char[inputStr.length()];
}
And, setting the attribute catchStr
to uninitialised. 并且,将属性
catchStr
设置为未初始化。
private char[] catchStr;
The second issue is with your for
loop. 第二个问题是你的
for
循环。 You start the counter
at 0 and use catchStr[counter - 1]
to look back on the previous character. 你从0开始
counter
并使用catchStr[counter - 1]
回顾前一个字符。 This will not work when counter = 0
because the index will evaluate to -1. 当
counter = 0
时,这将不起作用,因为索引将计算为-1。
Consider starting your counter
at 1, and continue to work on the method from there. 考虑从1开始
counter
,并从那里继续处理该方法。 You'll need to tweak it a little to get it output the desired string. 您需要稍微调整它以使其输出所需的字符串。
for (int counter = 1; counter < this.inputStr.length(); counter++) {
if (catchStr[counter] != catchStr[counter - 1]) {
modifiedStr = modifiedStr + catchStr[counter];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.