简体   繁体   English

如何解决此运行时错误?

[英]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 ,即字符串 “” 的大小 ,因此,您有一个大小为1char数组

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.

相关问题 需要帮助来了解运行时错误的原因以及如何解决它 - need help understanding the cause of the run-time error and how to fix it 以编程方式调用eclipse快速修复时运行时错误 - Run time error in when calling eclipse quick fix programmatically 如何解决运行时错误? - How to solve run time error? 美国宇航局Worldwind:你如何解决这些奇怪的运行时异常? - Nasa Worldwind: How do you fix these weird run time exceptions? 如何修复此错误无法在 Java 中运行程序“svn”错误 - How to fix this error Cannot run program "svn" Error in Java 如何使我的 java 程序正常运行并修复运行时错误? - How to make my java program run properly and fix runtime error? 如何修复“运行配置错误:[SDK中没有找到Rails]”? - How to fix “Run Configuration Error: [No Rails found in SDK]”? 如何修复错误处理计时器()在 kotlin 中运行多次? - How to fix error handling timer () run many times in kotlin? 每次运行程序时,如何解决NumberFormatException错误? - How do I fix the error of NumberFormatException everytime I run the program? 如何修复&#39;NotSerializableException:java.time.format.DateTimeFormatter&#39;错误 - How to fix 'NotSerializableException: java.time.format.DateTimeFormatter' Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM