2013-10-06 82 views
0

我是新來編碼,我一直在寫這段代碼,並試圖使其工作,但每次運行它崩潰。我已經查找了一些東西,並將編寫這段代碼,我已經遵循了java的網站,瞭解如何正確寫下代碼以及本網站。java.lang.ArrayIndexOutOfBoundsException:4錯誤

不管怎麼說,如果有人能向我解釋爲什麼這不起作用,因爲在我看來,邏輯在那裏,但我不明白它爲什麼會崩潰。

我的代碼:

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; 
      } 

    } 

主要方法似乎是很好,因爲它把一切都在isConsecutive方法,除了評論「返回true;」而且程序確實運行並打印爲真。所以我知道問題在於第二種方法。

如果有什麼我沒有做的權利請告訴我,那將不勝感激。畢竟我還在學習。

感謝

+0

的是有一個堆棧跟蹤? – Meistro

回答

0

你的所有來電arrayCharToInt[x+1]的打算出界對他們所在的循環的最後一次迭代(例如,如果arrayCharToInt.length等於5,最高的是x還將會持續下去是4.但是然後x+1等於5,這對於具有五個單元的數組而言是超出限制的)。你需要進行某種if(x == arrayCharToInt.length - 1)檢查。

在該方法isConsecutive內部的for循環
+0

它成功了,謝謝你,這就是我所做的,加上我加了'繼續'。跳過造成錯誤的循環 –

+0

太棒了,很高興它工作。對你而言,教你自己如何從教程中做到這一點很好(很多人沒有耐心!)。下一步,我強烈建議您簡化此代碼。實際上,您可以獲得與您擁有的代碼量的10%相同的結果。只要在字符串的每個字符上循環,並且對於不是第一個字符的每個字符,如果字符與前一個字符相同,則將'consecutiveOrNot'設置爲'true',並將'break'從循環中移出。如果這從來沒有發生,那麼你知道沒有連續的。這將會更加高效和可讀。 –

0

for (int x=0;x<arrayCharToInt.length ;x++),您已經使用arrayCharToInt[x+1]

如果arrayCharToInt lenth爲4,則你必須arrayCharToInt [0]arrayCharToInt [3]

現在考慮這樣的語句:arrayCharToInt[x+1] 當x是3本聲明將evalueate到arrayCharToInt[4]導致數組索引越界異常

相關問題