I get this error message:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -6 at java.lang.String.substring(Unknown Source) at ParseTheTweet.main(ParseTheTweet.java:41)
when trying to run my program. There are 24 lines of comments before the posted code. The entered tweet was:
#typ structure; #det damaged; #loc 224 left fork road (shed) (house okay); #lat 40.029854; #lng -105.391055;
public class Tweet {
public static void main(String[] args) {
Scanner theScanner = new Scanner(System.in);
String tweet, typ, det, loc, lat, lng;
System.out.println("Enter tweet here:");
tweet = theScanner.next();
int start, finish;
typ = tweet;
start = typ.indexOf("#")+ 5;
finish = typ.indexOf(";");
typ = typ.substring(start, finish);
typ = typ.trim();
tweet = tweet.substring(finish+1);
System.out.println("Type:" + "\t" + "\t" + typ);
What is wrong with my code?
typ, otherwise fetching a substring will not work properly, throwing the exception that you just saw.typ.indexOf("#")?