This is the full error message I received:
exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 27
at java.lang.String.charAt(String.java:646)
at Main.main(Main.java:268)
at Ideone.assertRegex(Main.java:110)
at Ideone.test(Main.java:42)
at Ideone.main(Main.java:28)
I wrote my program in DrJava for a class assignment and it works, but when I enter my code into their code runner I get the runtime error. My code is as follows:
import java.io.*;
import static java.lang.System.*;
import java.util.Scanner;
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 length = tweet.length();
if(length > 140){
System.out.println("Excess Characters: " + (length - 140));}
else{
System.out.println("Length Correct");
int countera = 0;
for( int i=0; i<tweet.length(); i++ ) {
if( tweet.charAt(i) == '#' ) {
i++;
if (( tweet.charAt(i) != ' ' ) && ( tweet.charAt(i) != '\t' ))
countera++;
}
}
int counterb = 0;
for( int i=0; i<tweet.length(); i++ ) {
if( tweet.charAt(i) == '@' ) {
i++;
if (( tweet.charAt(i) != ' ' ) && ( tweet.charAt(i) != '\t' ))
counterb++;
}
}
int counterc = 0 ;
String search = "http://";
for ( int i = -1 ; ( i = tweet.indexOf( search, i + 1 ) ) != -1 ; counterc ++ );
System.out.println("Number of Hashtags: " + countera);
System.out.println("Number of Attributions: " + counterb);
System.out.println("Number of Links: " + counterc);
}
}
}
Aucun commentaire:
Enregistrer un commentaire