简体   繁体   English

获取java.lang.StringIndexOutOfBoundsException

[英]Getting java.lang.StringIndexOutOfBoundsException

Disclaimer: I'm a beginner straight out of AP Computer Science. 免责声明:我是AP Computer Science的新手。

I'm trying to make an app that converts text input from the user into pig latin, for practice. 我正在尝试制作一个将用户输入的文本转换为拉丁文的应用程序,以进行练习。 It's a work in progress but so far i'm getting exceptions. 这是一项正在进行的工作,但到目前为止,我遇到了例外。

Here's my code: 这是我的代码:

package com.example.ashavolian.piglatin2;

import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity {

    TextView txtOut;
    EditText txtIn;
    Button button;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtOut = (TextView) findViewById(R.id.txtOut);
        txtIn = (EditText) findViewById(R.id.txtIn);
        button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String txtTxtIn = txtIn.getText().toString();
                txtOut.setText(translate(txtTxtIn));
            }
        });

    }

    public String translate(String txtTxtIn)
    {
        txtTxtIn += " "; 
        int length = txtTxtIn.length();
        String pigLatin = " ";

        for (int b = 0; b < length; b++) {
            int space = txtTxtIn.indexOf(" ");
            String word = txtTxtIn.substring(0, space);
            int a = word.indexOf("a");
            int e = word.indexOf("e");
            int i = word.indexOf("i");
            int o = word.indexOf("o");
            int u = word.indexOf("u");

            int vowels[] = {a, e, i, o, u};

            for (int c = 0; c < 4; c++)
            {
                if (vowels[c] == -1)
                    vowels[c] += 101;
            }

            for (int d = 0; d < 4; d++)
            {
                if (vowels[d] > -1 && vowels[d] <= vowels[0] && vowels[d] <= vowels[1] && vowels[d] <= vowels[2] && vowels[d] <= vowels[3] && vowels[d] <= vowels[4])
                {
                    word = word.substring(vowels[i] + 1) + word.substring(0,vowels[i]);
                    pigLatin += " " + word;
                }
            }
            txtTxtIn = txtTxtIn.substring(space + 1);
        }

        return pigLatin;
    }

}

The exception only occurs when you press the button. 仅当您按下按钮时才会发生例外。 Making the Program crash. 使程序崩溃。

Here's the exception: 这是例外:

FATAL EXCEPTION: main
Process: com.example.ashavolian.piglatin2, PID: 2991
java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=-1
    at java.lang.String.startEndAndLength(String.java:298)
    at java.lang.String.substring(String.java:1087)
    at com.example.ashavolian.piglatin2.MainActivity.translate(MainActivity.java:39)
    at com.example.ashavolian.piglatin2.MainActivity$1.onClick(MainActivity.java:27)
    at android.view.View.performClick(View.java:5198)
    at android.view.View$PerformClick.run(View.java:21147)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I suppose, an exception is thrown when you try to access element in the array with an index, which doesn't actually exist. 我想,当您尝试使用实际上不存在的索引访问数组中的元素时,将引发异常。 It's typical situation, when you get IndexOutOfBoundsException or StringIndexOutOfBoundsException . 当您获取IndexOutOfBoundsExceptionStringIndexOutOfBoundsException时,这是一种典型情况。

I cannot tell you in which line is a real mistake without debugging the application. 如果不调试应用程序,我无法告诉您哪一行是真正的错误。 Your translate(String) method is doing strange stuff and code is not so clean. 您的translate(String)方法正在做奇怪的事情,并且代码不是很干净。 Nevertheless, I have the following candidates, which could cause that exception: 但是,我有以下候选人,这可能会导致该异常:

  • vowels[i] - i variable is calculated by int i = word.indexOf("i"); vowels[i] i变量由int i = word.indexOf("i");计算得出i = word.indexOf("i"); and word is calculated by txtTxtIn.substring(0, space); word是由txtTxtIn.substring(0, space);计算的txtTxtIn.substring(0, space); , but you cannot be sure about length of txtxTxtIn , but max index of vovels is 4. When txtTxtIn has more characters than 5, an exception will be thrown ,但你不能肯定的长度txtxTxtIn ,但最大指数vovels是4.当txtTxtIn有5个以上的字符,一个异常将被抛出
  • txtTxtIn = txtTxtIn.substring(space + 1); - here may be similar story as above -这里的故事可能和上面类似

My suggestion is to debug this code. 我的建议是调试此代码。 Moreover, you could use List . 此外,您可以使用List It's Java and we have such structures here. 它是Java,我们这里有这样的结构。 It would be much easier to perform operations on strings and chars and harder to make such mistake. 在字符串和字符上执行操作会容易得多,而更容易出错。

Variable i in the following code is the problem. 以下代码中的变量i是问题所在。

word = word.substring(vowels[i] + 1) + word.substring(0,vowels[i]);

Check whether you want the variable i or some other variable. 检查您是否想要变量i或其他变量。

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

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