Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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.lang.ArrayIndexOutOfBoundsException:-1 公共类并行数组{ 公共静态扫描仪sc=新扫描仪(System.in); 公共静态void main(字符串[]args){ char[]charArray=新字符[5]; int[]intArray=新的int[5]; char ch; 整数计数; System.out.println(“输入5个字符:”); for(int i=0;i_Java_Arrays - Fatal编程技术网

“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:-1 公共类并行数组{ 公共静态扫描仪sc=新扫描仪(System.in); 公共静态void main(字符串[]args){ char[]charArray=新字符[5]; int[]intArray=新的int[5]; char ch; 整数计数; System.out.println(“输入5个字符:”); for(int i=0;i

“线程中的异常”;“主要”;java.lang.ArrayIndexOutOfBoundsException:-1 公共类并行数组{ 公共静态扫描仪sc=新扫描仪(System.in); 公共静态void main(字符串[]args){ char[]charArray=新字符[5]; int[]intArray=新的int[5]; char ch; 整数计数; System.out.println(“输入5个字符:”); for(int i=0;i,java,arrays,Java,Arrays,如果我为数组输入5个字符,a、b、c、d、e,然后要求用户输入另一组字符,我就输入a、b、c、d、y。它将退出程序并给出错误:-线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:-1 您没有检查用户输入不存在的字符的可能性 public class ParallelArray { public static Scanner sc = new Scanner(System.in); public static void m

如果我为数组输入5个字符,a、b、c、d、e,然后要求用户输入另一组字符,我就输入a、b、c、d、y。它将退出程序并给出错误:
-线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:-1

您没有检查用户输入不存在的字符的可能性

public class ParallelArray {

    public static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        char[] charArray = new char[5];
        int[] intArray = new int[5];
        char ch;
        int count;
        System.out.println("Enter 5 characters: ");
        for (int i = 0; i < charArray.length; i++) {
            charArray[i] = sc.next().charAt(0);
        }

        do {
            System.out.println("Enter a character: ");
            ch = sc.next().charAt(0);
            int location = search(ch, charArray);
            intArray[location]++;
            System.out.println("Again? 1-yes, 0-no");
            count = sc.nextInt();
        } while (count == 1);

        printBothLists(charArray, intArray);
    }

    public static void printBothLists(char[] charArray, int[] intArray) {
        for (int i = 0; i < charArray.length; i++) {
            System.out.println(charArray[i] + " - " + intArray[i]);
        }
    }

    public static int search(char ch, char[] charArray) {
        int count = -1;
        for (int i = 0; i < charArray.length; i++) {
            if (ch == charArray[i]) {
                count = i;
                return count;
            }
        }
        return count;
    }
}
int location=搜索(ch,charArray);
阵列[位置]++;//=0&&位置intArray[location]++;//
charArray[i]=sc.next().charAt(0);
与我有关。每次调用
sc.next()
都会使扫描仪通过您输入的任何一行文本。也许您需要一种更好的方法来拆分
字符串(
)(提示,提示)谢谢。您是对的,先生……我错过了这一部分。
int location = search(ch, charArray);
intArray[location]++; // <-- The index is invalid...
int location = search(ch, charArray);
if (location >= 0 && location < intArray.length) {
    intArray[location]++; // <-- The index is invalid...
} else {
    System.out.println("'" + ch + "' does not exist");
}