In my AP Computer Science class, we have an assignment where I need to input a string (which in this case is always a tweet) and then check to see if it meets the charlimit, see if it is a retweet, and then count the Hashtags and Mentions. I have figured all of this out, but for a Hashtag or mention to be counted, it has to not have a space or a return following it. My current solution is this:
for(int i=0; i < tweetLength; i++) {
if((tweet.charAt(i) == '@')&&((tweet.charAt(i+1) != 0)||(tweet.charAt(i+1) != 32)||(tweet.charAt(i+1) != 13)) ) {
countMentions++;
} if((tweet.charAt(i) == '#')&&((tweet.charAt(i+1) != 0)||(tweet.charAt(i+1) != 32)||(tweet.charAt(i+1) != 13)) ) {
countHashtags++;
} if(((tweet.charAt(i) == 'R')||(tweet.charAt(i) == 'r'))&&((tweet.charAt(i + 1) == 'T')||(tweet.charAt(i + 1) == 't'))&&(tweet.charAt(i + 2) == ':')) {
retweet = true;
}
}
Note, 32, 13, and 0 are ascii values for the Space, Return, and Null (I think, lol) -- I used the numerical values in hopes that it would miraculously solve my problems, but alas, it has not.
It all works fine, but when there is an Ampersand or a Hash sign at the very end of the string, then it returns with this error:
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.java:686)
at Main.main(Main.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
I know that this is caused because it is trying to read null, but I can't really find a solution and my teacher is the "Teach via. videos" kind, so not much help.