私の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);
}
}
}
ツイートが '#'、 '@'または 'h'のみで構成されている場合、'(ch ++)<=(tweet.length()) 'はどのようなブール値ですか? –
どのような入力文字列でランタイムエラーが発生しましたか?詳細を教えてください:私はいつも例外をスローしますか?charAt(ch ++)!= '' ' - 例外をスローします。あなたを走らせるrプログラム、それは仕事です。たぶんIDEの問題... – Alice
@Aliceプログラムを実行して、 '#'や '@'だけを入力してみてください。 –