-1

Here is input I am supposed to parse: #typ offer; #det free essential supplies 4 evacs pets.; #loc 2323 55th st, boulder; #lat 40.022;

This is my code: System.out.print("Enter a tweet in the correct format: "); String tweet = keyboard.nextLine();

    int start = tweet.indexOf("#");
    int finish = tweet.indexOf(";");
    String type = tweet.substring(start, finish);
    String type2 = type.substring(4,finish);
    String type_2 = type2.toUpperCase();



    String tweet2 = tweet.substring(finish + 1);
    int start2 = tweet2.indexOf("#");
    int finish2 = tweet2.indexOf(";");
    String detail = tweet2.substring(start2, finish2);
    String detail2 = tweet2.substring(5, finish2);



    String tweet3 = tweet2.substring(finish2 + 1);
    int start3 = tweet3.indexOf("#");
    int finish3 = tweet3.indexOf(";");
    String location = tweet3.substring(start3 , finish3);
    String location2 = tweet3.substring(4, finish3);

    /*
    String tweet4 = tweet3.substring(finish3 + 1);
    int start4 = tweet4.indexOf("#");
    int finish4 = tweet4.indexOf(";");
    String latitude = tweet4.substring(start4,  finish4);
    String latitude2 = tweet4.substring(4, finish4);


    String tweet5 = tweet4.substring(finish4 + 1);
    int start5 = tweet5.indexOf("#");
    int finish5 = tweet5.indexOf(";");
    String longitude = tweet5.substring(start5, finish5);
    String longitude2 = tweet5.substring(4, finish5);



    */
    System.out.println();
    System.out.println("Type: " + type_2);
    System.out.println("Detail: " + detail2);
    System.out.println("Location: " + location2);
    //System.out.println("Latitude: " + latitude2);
    //System.out.println("Longitude: " + longitude2);

    keyboard.close();

Ignoring the commented part this line, String location = tweet3.substring(start3 , finish3);, is giving me this error, 40.022;Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2 at java.lang.String.substring(Unknown Source) at ParseTheTweet.main(ParseTheTweet.java:31).

I commented out other parts to test my code on the given input and it works up until that point. Any ideas on why this is the case?

  • The exception explains exactly what is wrong - you are passing -2 into `substring`, which I presume (you should look it up, not me!) means that an earlier function call failed to find `#` or `;`. – Ken Y-N Sep 07 '17 at 03:09
  • Check as to whether any of the value for start3 or finish3 is a valid value and is not negative. – Soumya Sep 07 '17 at 03:09

2 Answers2

0

While your code is expecting longitude information, your input doesnt have longitude information and is the reason for StringIndexOutofBounds.

Enter some thing like, it should work.

typ offer; #det free essential supplies 4 evacs pets.; #loc 2323 55th st, boulder; #lat 40.022;#long 40.022;

Community
  • 1
  • 1
Anil
  • 595
  • 1
  • 5
  • 15
0

According to you input tweet4 = #lat 40.022; and finish4 = 12. As there is no longitude information in input string tweet4.substring(finish4 + 1) return empty as tweet5.

Moreover start5 and finish5 become -1. Hence tweet5.substring(start5, finish5) evaluated as "".substring(-1, -1) which caused StringIndexOutOfBoundsException.

So you have to add longitude information in your input text.

Shaiekh
  • 86
  • 5