简体   繁体   English

variable1.indexOf(variable2.charAt(i))超出范围异常

[英]variable1.indexOf(variable2.charAt(i)) Out of bounds exception

This is for my school assignment, and I'm supposed to create an encoder. 这是给我的学校作业,我应该创建一个编码器。 The user enters a encryption key which is contained in the variable 'key', an example of an encryption key is, "abc". 用户输入包含在变量“ key”中的加密密钥,加密密钥的示例为“ abc”。 Using the alphabet, I'm supposed to add the letters in the key the user entered according to the order of the alphabet. 使用字母,我应该根据字母顺序在用户输入的键中添加字母。 For example, the key "abc" is supposed to add to 6. So according to the order of the alphabet 'a' is 1, b = 2, c = 3, so 1 + 2 + 3 = 6. 例如,键“ abc”应该加到6。因此,根据字母“ a”的顺序为1,b = 2,c = 3,所以1 + 2 + 3 = 6。

Now for this error, whenever I try to add the alphabet.indexOf(key.charAt(i)) it gives an out of bounds exception. 现在针对此错误,每当我尝试添加alphabet.indexOf(key.charAt(i))时,它都会出现超出范围的异常。 How can I fix this? 我怎样才能解决这个问题?

 35   public static int option1(String input, String alphabet, String key)
 36     {
 37       
 38       int sum = 0;
 39         
 40       for (int i = 0; i < alphabet.length(); i++)
 41       { 
 42         
 43           **sum = sum + alphabet.indexOf(key.charAt(i));**
 44           
 45         }
 46                  
 47       System.out.println(sum);
 48        return sum;
 49     }

This gives an error of 这给出了一个错误

java.lang.StringIndexOutOfBoundsException: String index out of range: 3
    at java.lang.String.charAt(Unknown Source)
    at Encryption.option1(Encryption.java:43)
    at Encryption.main(Encryption.java:21)
    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:272)

The error is the sum statement where I add the index of a certain letter of the alphabet using a letter of another variable. 错误是sum语句,其中我使用另一个变量的字母添加某个字母的索引。 Why is this giving the out of bounds exception? 为什么这会超出范围? How can it be fixed? 如何解决?

If you have the key abc it can be broken down to a[0] b[1] c[2] so in the for loop when you get to i == 3 it is out of the range of your key. 如果您具有密钥abc则可以将其分解为a[0] b[1] c[2]因此在for循环中,当您达到i == 3它超出了密钥范围。 So you need to change your for loop to use key.length() rather than alphabet.length() like so: 因此,您需要将for循环更改for使用key.length()而不是key.length() alphabet.length()如下所示:

for (int i = 0; i < key.length(); i++)
{     
    sum = sum + alphabet.indexOf(key.charAt(i));        
}

I'm guessing you meant to use key.length() in your for loop. 我猜你打算在for循环中使用key.length()。

I guess i'll put my comment here too: 我想我也会在这里发表评论:

your "key" string is only length 3 if you input "abc"... However you are using "alphabet" length in your loop which is of length 26.. This is causing an error on trying to grab any character after the 3rd position of your string "key" 如果您输入“ abc”,则您的“键”字符串的长度仅为3。但是,您在循环中使用的长度为26的“字母”长度。这在尝试在第3个字符之后捕获任何字符时会导致错误字符串“键”的位置

 35   public static int option1(String input, String alphabet, String key)
 36     {
 37       
 38       int sum = 0;
 39         
 40       for (int i = 0; i < key.length(); i++)
 41       { 
 42         
 43           **sum = sum + alphabet.indexOf(key.charAt(i));**
 44           
 45         }
 46                  
 47       System.out.println(sum);
 48        return sum;
 49     }

The above answers hint at this as well, but it's important to note that when a String is created, it actually creates a reference to a 1-D array. 上面的答案也暗示了这一点,但重要的是要注意,当创建字符串时,它实际上会创建对一维数组的引用。 When creating or working with strings, it's good practice to think about what the size of the array is that's created rather than the size of the String (although in many cases this is interchangeable). 创建或使用字符串时,最好考虑创建的数组的大小而不是String的大小(尽管在很多情况下这是可以互换的)。 That way you can avoid any "ArrayOutOfBounds" exceptions that come up in the future. 这样,您可以避免将来出现任何“ ArrayOutOfBounds”异常。

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

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