I keep getting this error : Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 28 -


this code :

import java.util.scanner;  public class main {     public static void main(string[] args) {         scanner scan = new scanner(system.in);          string tweet = scan.nextline();          int link = 0;         int tag = 0;         int @ = 0;         int c = 0;         int l = tweet.length();          if (l <= 140) {             while (c < tweet.length()) {                 char let = tweet.charat(c);                 if (let == '#') {                     tag++;                     string hash = tweet.substring(c, c + 2);                     if (hash.equals("# ")) {                         tag--;                     }                  } else if (let == '@') {                     at++;                     string = tweet.substring(c, c + 2);                     if (to.equals("@ ")) {                         at--;                     }                 } else if (let == 'h') {                     string http = tweet.substring(c, c + 7);                     if (http.equals("http://")) {                         link++;                     }                 }                 c++;             }             system.out.println("number of hashtags: " + tag);             system.out.println("number of attributions: " + at);             system.out.println("number of links: " + link);         } else if (l > 140) {             int difference = tweet.length() - 140;             system.out.print("excess characters: " + difference);         }      } } 

every time run error:

exception in thread "main" java.lang.stringindexoutofboundsexception: string index out of range: 28     @ java.lang.string.substring(string.java:1951)     @ main.main(main.java:268)     @ ideone.assertregex(main.java:110) 

i wondering if there way fix it?

add range check before substring because tweet.substring(c, c + 2); might cause index out of bound error when, example, tweet length 7, c 6, c+2 8, exceeds length.

if (let == '#') {                 tag++;                 if (c < tweet.length() - 2) { // make sure c+2 in range substring function                     string hash = tweet.substring(c, c + 2);                     if (hash.equals("# ")) {                         tag--;                     }                 }              } else if (let == '@') {                 at++;                 if (c < tweet.length() - 2) {// make sure c+2 in range substring function                     string = tweet.substring(c, c + 2);                     if (to.equals("@ ")) {                         at--;                     }                 }             } else if (let == 'h') {                 if (c < tweet.length() - 7) {// make sure c+7 in range substring function                     string http = tweet.substring(c, c + 7);                     if (http.equals("http://")) {                         link++;                     }                 }             } 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -