Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java程序运行时错误ArrayIndexOutofBounds_Java - Fatal编程技术网

Java程序运行时错误ArrayIndexOutofBounds

Java程序运行时错误ArrayIndexOutofBounds,java,Java,这是我的密码。我不确定出了什么问题。我的项目是创建一个程序来检查一个单词是否是回文 import java.util.Scanner; public class PalindromeChecker { public static void main(String[] args) { // Open Scanner Scanner input = new Scanner(System.in); // Prompt User for word

这是我的密码。我不确定出了什么问题。我的项目是创建一个程序来检查一个单词是否是回文

import java.util.Scanner;

public class PalindromeChecker {
    public static void main(String[] args) {
        // Open Scanner
        Scanner input = new Scanner(System.in);
        // Prompt User for word
        System.out.println("Enter word to check if it is a Palindrome:");
        // Scan in the word
        String word = input.nextLine();
        int a = 0; // used to extract word from array (small)
        int b = 0; // used to extract word from array (large)
        int c = 0; // used to stop when
        int d = (word.length());
        int e = d / 2;
        int f = e - 2;
        int x = word.length(); // set cap of array pulling
        char[] array = word.toCharArray(); // create array of chars
        if (array[a] == array[x] && c != f) {
            a++;
            x--;
            c++;
        } else {
            b = 1;
        }
        if (b == 1) {
            System.out.println("This word is not a Palindrome!");
        } else if (b == 0) {
            System.out.println("This word is a Palindrome!");
        }
    }
}
错误就在下面

if (array[a] == array[x] && c!=f)

我不确定哪里出了问题,但当你输入一个非回文时,它会跳过。我非常乐意就这种情况下该怎么办提供一些建议。

因为数组是基于0的,所以最后一个条目的索引是length-1

int x = word.length() - 1
您还缺少检查单词中所有字符的循环。最后,你似乎有很多多余的变量。以下是修复代码的方法:

    boolean isPalindrome = true;
    for (int n = 0; n < array.length / 2; n++){
        if (array[n] != array[array.length - n - 1]) {
            isPalindrome = false;
            break;
        }
    }
    if (isPalindrome) {
        System.out.println("This word is a Palindrome!");
    } else {
        System.out.println("This word is not a Palindrome!");
    }
boolean isPalindrome=true;
对于(int n=0;n
因为数组是基于0的,所以最后一个条目的索引是length-1

int x = word.length() - 1
您还缺少检查单词中所有字符的循环。最后,你似乎有很多多余的变量。以下是修复代码的方法:

    boolean isPalindrome = true;
    for (int n = 0; n < array.length / 2; n++){
        if (array[n] != array[array.length - n - 1]) {
            isPalindrome = false;
            break;
        }
    }
    if (isPalindrome) {
        System.out.println("This word is a Palindrome!");
    } else {
        System.out.println("This word is not a Palindrome!");
    }
boolean isPalindrome=true;
对于(int n=0;n
如果您能命名变量就好了。@schmosel知道了。一秒钟。你应该应用于检查数组字符的循环在哪里?如果你给变量命名就好了。@schmosel知道了。一秒钟。你应该应用于检查数组字符的循环在哪里?我尝试了这个。它解决了大多数问题,但现在当我使用racecafr时(我是通过打字错误得到的),它不起作用。你还有什么建议吗?你只检查第一封和最后一封信。你应该用一个循环来检查所有的。我试过了。它解决了大多数问题,但现在当我使用racecafr时(我是通过打字错误得到的),它不起作用。你还有什么建议吗?你只检查第一封和最后一封信。您应该使用一个循环来检查所有。