我一直在越界界错误如何解决这个问题?

时间:2022-08-25 16:39:06
Scanner scan = new Scanner(System.in);
System.out.println("Enter a word");
String line = scan.nextLine();
String pig = "";
int a = line.length();

int x = 0;
int y = 0;

while (y < a) {
    int b = line.indexOf(" ");

    if (line.substring(x, x + 1).equals("a") || 
            line.substring(x, x + 1).equals("e") || 
            line.substring(x, x + 1).equals("i") || 
            line.substring(x, x + 1).equals("o") || 
            line.substring(x, x + 1).equals("u"))
    {
        pig = line.substring(x, b - 1);
    }

    else {
        pig = line.substring(b - 1, x + 2) + line.charAt(x);
    }

    line = line.substring(b + 1);
    y++;
}

System.out.println(pig);

I'm suppose to convert the words in any sentence into pig latin if the word starts with a vowel just add way at the end if it starts with a consonant bring the first letter to the back and add way

我想把任何一个句子中的单词转换为猪拉丁语如果单词以元音开头只是在末尾添加方式,如果它以辅音开头,将第一个字母带到后面并添加方式

Stacktrace:

java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at
java.lang.String.substring(Unknown Source) at pig_latin.main(Firstname_Lastname.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at
java.lang.reflect.Method.invoke(Unknown Source) at
edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:27‌​2

3 个解决方案

#1


2  

Your method for tracking the words in the line is a problem use String.split(" ") to break the line into a series of words and then your while loop can be

跟踪行中单词的方法是一个问题,使用String.split(“”)将行分成一系列单词然后你的while循环可以

String[] words = line.split(" ");
for(String word:words) {
   // do pig latin on word
}

I suspect it's the substring on line that's failing as you don't terminate correctly when you've processed all words because the variable y is based on characters, where your algorithm is chunking words.

我怀疑这是因为你在处理完所有单词时没有正确终止因为你的算法是分块单词的字符,所以在线的子字符串失败了。

#2


1  

You problem is

你的问题是

int b = line.indexOf(" ");

If you read the java docs. It will return -1 if string is not found. So if b becomes -1 then these lines will be evaluated like:

如果您阅读了java文档。如果找不到字符串,它将返回-1。因此,如果b变为-1,那么这些行将被评估为:

pig = line.substring(x, -2);

or

pig = line.substring(-2, x + 2) + line.charAt(x);

#3


1  

When you call pig = line.substring(x, b - 1); you are assuming that b is initialized with a positive int, which would only be true if when you evaluate int b = line.indexOf(" "); there actually was a space in the String. If there wasn't, b's value will be -1, and when you subtract an additional 1 from that, you are attempting to call subString with a value that is out of the String's bounds.

当你调用pig = line.substring(x,b - 1)时;你假设b用一个正整数初始化,只有当你评估int b = line.indexOf(“”)时才会这样; String中实际上有一个空格。如果没有,则b的值为-1,当你从中减去1时,你试图用超出String的界限的值调用subString。

#1


2  

Your method for tracking the words in the line is a problem use String.split(" ") to break the line into a series of words and then your while loop can be

跟踪行中单词的方法是一个问题,使用String.split(“”)将行分成一系列单词然后你的while循环可以

String[] words = line.split(" ");
for(String word:words) {
   // do pig latin on word
}

I suspect it's the substring on line that's failing as you don't terminate correctly when you've processed all words because the variable y is based on characters, where your algorithm is chunking words.

我怀疑这是因为你在处理完所有单词时没有正确终止因为你的算法是分块单词的字符,所以在线的子字符串失败了。

#2


1  

You problem is

你的问题是

int b = line.indexOf(" ");

If you read the java docs. It will return -1 if string is not found. So if b becomes -1 then these lines will be evaluated like:

如果您阅读了java文档。如果找不到字符串,它将返回-1。因此,如果b变为-1,那么这些行将被评估为:

pig = line.substring(x, -2);

or

pig = line.substring(-2, x + 2) + line.charAt(x);

#3


1  

When you call pig = line.substring(x, b - 1); you are assuming that b is initialized with a positive int, which would only be true if when you evaluate int b = line.indexOf(" "); there actually was a space in the String. If there wasn't, b's value will be -1, and when you subtract an additional 1 from that, you are attempting to call subString with a value that is out of the String's bounds.

当你调用pig = line.substring(x,b - 1)时;你假设b用一个正整数初始化,只有当你评估int b = line.indexOf(“”)时才会这样; String中实际上有一个空格。如果没有,则b的值为-1,当你从中减去1时,你试图用超出String的界限的值调用subString。