0

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?

4
  • What happens when neither a hash mark nor a semi-colon appear in your string? Commented Sep 14, 2014 at 23:55
  • 1
    You should check whether both '#' and ';' occur in typ, otherwise fetching a substring will not work properly, throwing the exception that you just saw. Commented Sep 14, 2014 at 23:57
  • And why exactly are you adding 5 to typ.indexOf("#")? Commented Sep 14, 2014 at 23:58
  • What is wrong?? You tried to address character -6 of a string. The exception info tells you what line this happened on, so it's easy to add println statements (or simply use your debugger) to see what's going on prior to that point. Trace backwards to your error. (This is known as "debugging".) Commented Sep 15, 2014 at 0:55

1 Answer 1

1

I was able to invoke a similar error condition in which I selected a positive start substring, but had -1 as my ending substring.

In this case, it would be caused by your finish variable containing a value of -1, meaning that the semi-colon wasn't found.

...And the reason for that is because you're using Scanner#next(), which will only ever consume up until the next whitespace value.

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

From Scanner.next():

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern.

In this case, the delimiter pattern is all whitespace, since you didn't change the default behavior.

To make things a bit simpler, change that to use nextLine() instead.

Sign up to request clarification or add additional context in comments.

3 Comments

... and use hasNext() before calling next(). (the same apply with hasNextLine).
@NoDataFound: I could see using that if this were reading from a file, but since it's reading from STDIN, the execution of the program will actually block until it reads a newline character. So, I wouldn't see much purpose behind the hasNextLine in this case.
I said that for the sake of completion, and because he could (if he needs it) transform the try {} catch(NoSuchElementException) into a boolean test and do something (like asking for a valid number, etc).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.