2013-08-31 9 views
5

私のコードです:配列インデックスアウト(Java)の

public class countChar { 

    public static void main(String[] args) { 
     int i; 
     String userInput = new String(); 

     userInput = Input.getString("Please enter a sentence"); 

     int[] total = totalChars(userInput.toLowerCase()); 

     for (i = 0; i < total.length; i++); 
     { 
      if (total[i] != 0) { 
       System.out.println("Letter" + (char) ('a' + i) + " count =" + total[i]); 
      } 
     } 
    } 

    public static int[] totalChars(String userInput) { 
     int[] total = new int[26]; 
     int i; 
     for (i = 0; i < userInput.length(); i++) { 
      if (Character.isLetter(userInput.charAt(i))) { 
       total[userInput.charAt(i) - 'a']++; 
      } 
     } 
     return total; 
    } 
} 

プログラムの目的は、文字列をユーザーに尋ね、その後、各文字が文字列で使用された回数をカウントすることです。

私がプログラムをコンパイルすると、正常に動作します。私はプログラムを実行すると、私はポップアップボックスに文字列を入力することができるが、私は、文字列を提出し、OKを押した後、私は

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 26 
at countChar.main(countChar.java:14) 

は私が何を問題に完全にはわからないと言って、エラーを取得しますそれをどうやって修正するかです。このセミコロンで

+6

私は 'for'ループは' if'文を囲むべきで、最後の ';'は間違っていると思います。 –

+0

14行目の行を指定してください。 ( '// < - ArrayIndexOutOfBoundsException here'を使用してください) – Stewart

答えて

14
for (i = 0; i < total.length; i++); 
            ^-- remove the semi-colon here 

は、ループが何もしない、i == total.lengthまでループし、次に何を思ったことはループの本体が実行されました。

+1

' {'(' for'行の最後に書かれている)の書式が違っていると感じました。 – Stewart

+0

私はこのコードを更新しました: 'countChar.java:18:error:不正な式の開始 public static int [] totalChars(String userInput) ^'このエラーは、 2番目のメソッド宣言行。 – Boxasauras

+1

'for(i = 0;!compiler.someoneFixesTheCompilerToWarnAboutThisError(); i ++)StackOverflow.answerQuestionBySomeoneToMakeThisSameErrorAgain();' iの現在の値は何ですか?私はそれが今何千もあると思う... – ajb

6
for (i = 0; i < total.length; i++); // remove this 
{ 
    if (total[i]!=0) 
     System.out.println("Letter" + (char)('a' + i) + " count =" + total[i]); 
} 

次いでi=26までループループのザ(26 total.lengthである)あなたのifは、アレイの境界上に行く、実行されます。 forループの最後に;を削除します。

0

あなたはこの

for (i = 0; i < total.length; i++); 
+0

これは質問への答えを提供しない。批評をしたり、著者の説明を求めるには、自分の投稿の下にコメントを残してください。自分の投稿にいつもコメントをつけることができます。そして、十分な[評判](http://stackoverflow.com/help/whats-reputation) [任意の投稿にコメントする]ことができます(http://stackoverflow.com/help/privileges/comment)。 – paqogomez

+0

私はこのソーシャルネットワークの新機能です。私は、質問を掲示して回答するための規則を読みます。ありがとうございました! –

0

これは、Javaでのアレイのマイナス長さのとても良い例である私は、ここで、この私< arrayOFInt.length上記の両方の例

public static int linearSearchArray(){ 

    int[] arrayOFInt = {1,7,5,55,89,1,214,78,2,0,8,2,3,4,7}; 
    int key = 7; 
    int i = 0; 
    int count = 0; 
    for (i = 0; i< arrayOFInt.length; i++){ 
     if (arrayOFInt[i] == key){ 
     System.out.println("Key Found in arrayOFInt = " + arrayOFInt[i]); 
     count ++; 
     } 
    } 

    System.out.println("this Element found the ("+ count +") number of Times"); 
return i; 
} 

を与えているを削除する必要があります。配列の長さで1を減算する必要はありません。 しかし、もし私が< = arrayOFInt.length -1;他の賢明な必要がありますarrayOutOfIndexException発生します、これがあなたを助けてくれることを願っています。

-1
import java.io.*; 
import java.util.Scanner; 
class ar1 { 
    public static void main(String[] args) { 
     //Scanner sc=new Scanner(System.in); 
     int[] a={10,20,30,40,12,32}; 
     int bi=0,sm=0; 
     //bi=sc.nextInt(); 
     //sm=sc.nextInt(); 
     for(int i=0;i<=a.length-1;i++) { 
      if(a[i]>a[i+1]) 
       bi=a[i]; 

      if(a[i]<a[i+1]) 
       sm=a[i]; 
     } 
     System.out.println("big"+bi+"small"+sm); 
    } 
} 
+2

単純にコードのブロックを投稿するのではなく、答えを説明してください – avojak