Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.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:4错误_Java_Arrays_Indexoutofboundsexception - Fatal编程技术网

java.lang.ArrayIndexOutOfBoundsException:4错误

java.lang.ArrayIndexOutOfBoundsException:4错误,java,arrays,indexoutofboundsexception,Java,Arrays,Indexoutofboundsexception,我对编码是新手,我一直在写这段代码,并试图让它工作,但每次我运行它时,它都崩溃了。我已经查找了一些东西,并将编写这段代码。我跟随java的网站了解了如何正确地编写代码以及这个网站 无论如何,如果有人能向我解释为什么这不起作用,我将不胜感激,因为在我看来,逻辑是存在的,但我不明白为什么它会崩溃 我的代码: import java.util.Scanner; import java.lang.String; import java.util.*; public cla

我对编码是新手,我一直在写这段代码,并试图让它工作,但每次我运行它时,它都崩溃了。我已经查找了一些东西,并将编写这段代码。我跟随java的网站了解了如何正确地编写代码以及这个网站

无论如何,如果有人能向我解释为什么这不起作用,我将不胜感激,因为在我看来,逻辑是存在的,但我不明白为什么它会崩溃

我的代码:

    import java.util.Scanner;
    import java.lang.String;
    import java.util.*;
    public class Question1
    {
      public static void main(String[] args)
          {
             Scanner keyboard= new Scanner(System.in);
             System.out.println("Enter either letters or numbers and I'll magically tell you if they are consecutive :D");
             String inputedString= keyboard.nextLine();
             boolean consecutiveOrNot=isConsecutive(inputedString);
             System.out.println("Drum rolls...... Is it consecutive: "+ consecutiveOrNot);  //Problem with this line?
          }


      public static boolean isConsecutive(String inputedString)
          {
            //Storing string's units into an array and converting to UpperCase if necessary
            //and storing string's numerical value into the variable 'arrayCharToInt'
              char[] charIntoArray= new char[inputedString.length()];
              int[] arrayCharToInt= new int[inputedString.length()];
              for (int i=0;i<inputedString.length();i++ )
                {
                   charIntoArray[i]=inputedString.charAt(i);
                    if (Character.isLetter(charIntoArray[i]) && Character.isLowerCase(charIntoArray[i]))
                     {
                        charIntoArray[i]= Character.toUpperCase(charIntoArray[i]);
                      }
                    arrayCharToInt[i]=(int) charIntoArray[i];
                }




           // The next if statements and the methods that they call are used to verify 
           //that the content of the initial string is either letters or numbers, but not both together
              boolean[] continuous= new boolean[arrayCharToInt.length];
              boolean[] testContNumbersDecreasing= new boolean[arrayCharToInt.length];
              boolean[] testContNumbersIncreasing= new boolean[arrayCharToInt.length];
              boolean[] testContLettersDecreasing= new boolean[arrayCharToInt.length];
              boolean[] testContLettersIncreasing= new boolean[arrayCharToInt.length];
              Arrays.fill(continuous, true);
               if (lowestValue(arrayCharToInt)>=65 && highestValue(arrayCharToInt)<= 90)
                {
                    for (int x=0;x<arrayCharToInt.length ;x++ ) 
                    {
                       testContLettersIncreasing[x]=((arrayCharToInt[x+1]-arrayCharToInt[x]== 1) || (arrayCharToInt[x+1]-arrayCharToInt[x]== -25));
                       testContLettersDecreasing[x]=((arrayCharToInt[x]-arrayCharToInt[x+1]== 1) || (arrayCharToInt[x]-arrayCharToInt[x+1]== -25));
                    }
                    return (Arrays.equals(continuous,testContLettersIncreasing) || Arrays.equals(continuous,testContLettersDecreasing));
                }

               else if ((lowestValue(arrayCharToInt) >= 48) && (highestValue(arrayCharToInt)<= 57))
                {
                    for (int x=0;x<arrayCharToInt.length ;x++ ) 
                    {
                       testContNumbersIncreasing[x]=((arrayCharToInt[x+1]-arrayCharToInt[x]== 1) || (arrayCharToInt[x+1]-arrayCharToInt[x]== -9));
                       testContNumbersDecreasing[x]=((arrayCharToInt[x]-arrayCharToInt[x+1]== 1) || (arrayCharToInt[x]-arrayCharToInt[x+1]== -9));
                    }
                    return (Arrays.equals(continuous,testContNumbersIncreasing) || Arrays.equals(continuous,testContNumbersDecreasing));

                }
              else
                {
                    return false;
                }

          }



      public static int lowestValue(int[] array)
          {
                int lowest=array[0];
                  for (int counter=0; counter< array.length; counter++)
                    {
                      if( lowest>array[counter])
                            lowest= array[counter];
                    }
                    return lowest;
          }

      public static int highestValue(int[] array)
          {
               int highest=array[0];
                for (int counter=0; counter< array.length; counter++)
                    {
                      if( highest<array[counter])
                        highest= array[counter];
                    }
                return highest;
          }

    }
import java.util.Scanner;
导入java.lang.String;
导入java.util.*;
公开课问题1
{
公共静态void main(字符串[]args)
{
扫描仪键盘=新扫描仪(System.in);
System.out.println(“输入字母或数字,我会神奇地告诉您它们是否连续:D”);
String InputString=keyboard.nextLine();
布尔连续或非连续=连续(输入字符串);
System.out.println(“滚筒转鼓……是连续的:“+连续的还是不连续的”);//这条线有问题吗?
}
公共静态布尔值是连续的(字符串输入字符串)
{
//将字符串的单位存储到数组中,并在必要时转换为大写
//并将字符串的数值存储到变量“ArrayChartPoint”中
char[]charIntoArray=新字符[InputString.length()];
int[]ArrayChartPoint=new int[InputString.length()];

对于(int i=0;i=65&&highestValue(ArrayChartPoint)您对
ArrayChartPoint[x+1]
的所有调用都将在所处循环的最后一次迭代中超出范围(例如,如果
ArrayChartPoint.length
等于5,则
x
的最大值为4。但是
x+1
等于5,这对于一个有五个单元格的数组来说是不允许的)。您需要输入某种
if(x==ArrayChartPoint.length-1)
检查。

方法中的for循环内是连续的:
for(int x=0;x当数组调用函数出错时,将引发此错误。
你得到了长度并打印出来。
例如:

此数组的长度为

int length = a.length
所以
length=4
但最高索引是3,而不是4。这意味着数组的索引从0开始。因此您必须打印:

arr[length-1];
在你的节目里,

x == arrayCharToInt.length - 1

有堆栈跟踪吗?它成功了谢谢,这就是我所做的,另外我还添加了“continue;”以跳过使错误变得伟大的循环,很高兴它成功了。这对你从教程中自学如何做到这一点很有好处(许多人没有耐心!)。下一步,我强烈建议您简化此代码。实际上,您可以使用大约10%的代码量获得相同的结果。只需在字符串的每个字符上循环,对于不是第一个字符的每个字符,如果该字符与前面的字符相同,请将
连续或不
设置为
,并将
从循环中分离出来。如果这种情况从未发生,那么您就知道没有连续性。这将更加高效和可读。
x == arrayCharToInt.length - 1