简体   繁体   中英

Array out of bounds in Java?

I'm trying to run this code but I keep getting an out of bounds error. This is just the sub class for the super class "Simpler." The user enters in a string then the string is broken down into a char array. The array should not be smaller than the string yet I am getting this error. Can you tell me what I'm doing wrong? Thanks!

import java.util.*;
public class Encrypt extends Simpler
{
    public void encryption()
    {
        boolean loop = true;
        while(loop==true)
        {
            Scanner scan = new Scanner(System.in);

            System.out.println("Please enter the phrase you'd like to encrypt: ");
            String inPhrase = scan.nextLine();
            char[] chars = inPhrase.toCharArray();
            char tempArray[] = new char[chars.length+1];
            tempArray = chars;
            chars = new char[tempArray.length];
            for (int i = inPhrase.length(); i<inPhrase.length(); i--)
            {
                if(chars[i]=='a')
                {
                    chars[i]='1';
                }
                else if(chars[i]=='b')
                {
                    chars[i]='2';
                }
                else if(chars[i]=='c')
                {
                    chars[i]='3';
                }
                else if(chars[i]=='d')
                {
                    chars[i]='4';
                }
                else if(chars[i]=='z')//I skipped some lines here for convienence
                {
                    chars[i]='{';
                }
                else if(chars[i]==' ')
                {
                    chars[i]='}';
                }
            }
            String outPhrase = new String(chars);
            System.out.println(outPhrase);
        }
    }
}

I think your for loop statement should look like this:

for (int i = 0; i < inPhrase.length(); i++)

if you're counting up, and like this:

for (int i = inPhrase.length() - 1; i >= 0; i--)

if you're counting down.

update

On looking back over it, I think there is more to it than that though. I think your code needs to be rewritten:

String inPhrase = scan.nextLine();
char[] chars = inPhrase.toCharArray();
char tempArray[] = new char[chars.length()];

for (int i = 0; i < chars.length(); i++)
{
    if(chars[i]=='a')
    {
        tempArray[i]='1';
    }
.
.
.
.
}
String outPhrase = new String(tempArray);

No stop condition in the for loop, in this line:

for (int i = inPhrase.length(); i<inPhrase.length(); i--)

i gets 1, 0, -1, ... and wouldn't stop if -1 wouldn't throw an out of bounds exception

在for循环中,只需将条件从i < inPhrase.length()更改为i >= 0就可以了。

First thing:

for (int i = inPhrase.length(); i<inPhrase.length(); i--)

you never enter your loop because you assign i = n & entry condition is i < n.

it should be

for (int i = inPhrase.length()-1; i>=0; i--)

Now, this also removes your arrayoutofbound exception because earlier, you tried to access chars[n] which is actually the n+1 th character of that array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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