2016-03-30 8 views
0

私のAPコンピュータサイエンスの宿題を今行っていますが、ランタイムエラーが発生しています。誰かが私のコードに何が間違っているか知っていますか? ch++のCHの値がこの条件(ch++)<=(tweet.length())をチェックした後インクリメントなっているなぜ実行時エラーが発生しますか:StringIndexOutOfBounds?

class Main{ 

public static void main (String str[]) throws IOException { 
    Scanner scan = new Scanner(System.in); 

    System.out.println("Please enter a tweet:"); 
    String tweet = scan.nextLine(); 

    int hash = 0; 
    int attr = 0; 
    int link = 0; 
    int ch = 0; 
    if(tweet.length()>140) 
    { 
    System.out.println("Excess Characters: " + (tweet.length() - 140)); 
    } 

    else 
    { 
    tweet=tweet.toLowerCase(); 
    System.out.println("Length Correct"); 


    for(ch=0; ch<tweet.length(); ch++) 
    { 
    if(tweet.charAt(ch) == '#' && ((ch++)<=(tweet.length())) && (tweet.charAt(ch++)!=' ' && tweet.charAt(ch++)!='\t')) 
    { 
    hash++; 
    } 
    if(tweet.charAt(ch) == '@' && ((ch++)<=(tweet.length())) && (tweet.charAt(ch++)!=' ' && tweet.charAt(ch++)!='\t')) 
    { 
    attr++; 
    } 
    if(tweet.charAt(ch) == 'h' && ((ch + 7)<=(tweet.length()))) 
    { 
    String a = new String("http://"); 
    String sub = new String(tweet.substring(ch, ch + 7)); 
    if (sub.equals(a)) 
    {link++;} 
    } 



    } 

    System.out.println("Number of Hashtags: " + hash); 
    System.out.println("Number of Attributions: " + attr); 
    System.out.println("Number of Links: " + link); 

    } 

} 
} 
+1

ツイートが '#'、 '@'または 'h'のみで構成されている場合、'(ch ++)<=(tweet.length()) 'はどのようなブール値ですか? –

+0

どのような入力文字列でランタイムエラーが発生しましたか?詳細を教えてください:私はいつも例外をスローしますか?charAt(ch ++)!= '' ' - 例外をスローします。あなたを走らせるrプログラム、それは仕事です。たぶんIDEの問題... – Alice

+0

@Aliceプログラムを実行して、 '#'や '@'だけを入力してみてください。 –

答えて

1

ので... プログラムはDr.Javaに罰金を働いていたが、それはedhesiveで私のウェブサイトのテスター上で実行時エラーが表示されます。

説明:

  1. tweet.charAt(CH)CH = 0
  2. ((CH ++)<:

    if(tweet.charAt(ch) == '#' && ((ch++)<=(tweet.length())) && (tweet.charAt(ch++)!=' ' && tweet.charAt(ch++)!='\t')) 
        { 
        hash++; 
        } 
    

    上記コードについて(I = 0)4つの条件が存在します=(tweet.length()))ch = 0、しかしch ++だから、値は条件チェックの後にインクリメントされるでしょう。

  3. (tweet.charAt(CH ++)CH = 1(ポイント番号のbecuase 2)
  4. tweet.charAt(CH ++)CH = 2(同じ理由)

この試してください。

class Main{ 

public static void main (String str[]) throws IOException { 
    Scanner scan = new Scanner(System.in); 

    System.out.println("Please enter a tweet:"); 
    String tweet = scan.nextLine(); 

    int hash = 0; 
    int attr = 0; 
    int link = 0; 
    int ch = 0; 
    if(tweet.length()>140) 
    { 
    System.out.println("Excess Characters: " + (tweet.length() - 140)); 
    } 

    else 
    { 
    tweet=tweet.toLowerCase(); 
    System.out.println("Length Correct"); 


    for(ch=0; ch<tweet.length(); ch++) 
    { 
    if(tweet.charAt(ch) == '#' && ((ch+1)<(tweet.length())) && (tweet.charAt(ch+1)!=' ' && tweet.charAt(ch+1)!='\t')) 
    { 
    hash++; 
    } 
    if(tweet.charAt(ch) == '@' && ((ch+1)<(tweet.length())) && (tweet.charAt(ch+1)!=' ' && tweet.charAt(ch+1)!='\t')) 
    { 
    attr++; 
    } 
    if(tweet.charAt(ch) == 'h' && ((ch + 7)<(tweet.length()))) 
    { 
    String a = new String("http://"); 
    String sub = new String(tweet.substring(ch, ch + 7)); 
    if (sub.equals(a)) 
    {link++;} 
    } 



    } 

    System.out.println("Number of Hashtags: " + hash); 
    System.out.println("Number of Attributions: " + attr); 
    System.out.println("Number of Links: " + link); 

    } 

} 
} 
+0

あなたのコードは長さ1のつぶやきに失敗します。たとえば、 '#'や '@'だけのつぶやきは、範囲外のインデックス例外をスローします。 'ch = 0'のとき、ツイートの長さが1であるので、' ch + 1 <= tweet.length'と 'tweet.charAt(ch + 1)'は例外をスローする。 –

+0

thnx ..答えを編集した。 –

+0

はい。それはうまくいく。私はch ++とch + 1について混乱しましたが、今はかなり明確です。ありがとうたくさん:) –

関連する問題