简体   繁体   English

为什么我得到运行时错误:StringIndexOutOfBounds?

[英]why am I getting run time error:StringIndexOutOfBounds?

Doing my AP Computer Science homework right now but I am stuck with run-time errors. 现在正在做我的AP Computer Science作业,但是我遇到了运行时错误。 Does anyone know what is wrong with my code? 有人知道我的代码有什么问题吗? The program was working fine on Dr.Java but it shows run-time error on my website tester in edhesive... 该程序在Dr.Java上运行正常,但在我的网站测试仪上显示了运行时错误,但使用了粘合剂。

class Main{

 public static void main (String str[]) throws IOException {
   Scanner scan = new Scanner(System.in);

   System.out.println("Please enter a tweet:");
   String tweet = scan.nextLine();

   int hash = 0;
   int attr = 0;
   int link = 0;
   int ch = 0;
   if(tweet.length()>140)
   {
   System.out.println("Excess Characters: " + (tweet.length() - 140 ));
   }

   else
   {
   tweet=tweet.toLowerCase();
   System.out.println("Length Correct");


   for(ch=0; ch<tweet.length(); ch++)
   {
   if(tweet.charAt(ch) == '#' && ((ch++)<=(tweet.length())) && (tweet.charAt(ch++)!=' ' && tweet.charAt(ch++)!='\t'))
   {
   hash++;
   }
   if(tweet.charAt(ch) == '@' && ((ch++)<=(tweet.length())) && (tweet.charAt(ch++)!=' ' && tweet.charAt(ch++)!='\t'))
   {
   attr++;
   }
   if(tweet.charAt(ch) == 'h' && ((ch + 7)<=(tweet.length())))
   {
   String a = new String("http://");
   String sub = new String(tweet.substring(ch, ch + 7)); 
     if (sub.equals(a))
     {link++;}
   }



   }

   System.out.println("Number of Hashtags: " + hash);
   System.out.println("Number of Attributions: " + attr);
   System.out.println("Number of Links: " + link);

   }

}
}

Because of ch++ the value of ch is getting incremented after checking this condition (ch++)<=(tweet.length()) . 由于ch++ ,在检查此条件(ch++)<=(tweet.length())之后, ch++的值将递增。

Explanation : 说明:

if(tweet.charAt(ch) == '#' && ((ch++)<=(tweet.length())) && (tweet.charAt(ch++)!=' ' && tweet.charAt(ch++)!='\t'))
   {
   hash++;
   }

For above code there are 4 conditions (for i=0): 对于上面的代码,有4个条件(对于i = 0):

  1. tweet.charAt(ch) ch = 0 tweet.charAt(ch)ch = 0
  2. ((ch++)<=(tweet.length())) ch = 0, but ch++ so the value will be increamented after the condition check. ((ch ++)<=(tweet.length()))ch = 0,但ch ++,因此在条件检查之后该值将消失。
  3. (tweet.charAt(ch++) ch=1 (becuase of Point No. 2) (tweet.charAt(ch ++)ch = 1(由于第2点)
  4. tweet.charAt(ch++) ch = 2 (for the same reason). tweet.charAt(ch ++)ch = 2(出于相同的原因)。

Try this: 尝试这个:

class Main{

 public static void main (String str[]) throws IOException {
   Scanner scan = new Scanner(System.in);

   System.out.println("Please enter a tweet:");
   String tweet = scan.nextLine();

   int hash = 0;
   int attr = 0;
   int link = 0;
   int ch = 0;
   if(tweet.length()>140)
   {
   System.out.println("Excess Characters: " + (tweet.length() - 140 ));
   }

   else
   {
   tweet=tweet.toLowerCase();
   System.out.println("Length Correct");


   for(ch=0; ch<tweet.length(); ch++)
   {
   if(tweet.charAt(ch) == '#' && ((ch+1)<(tweet.length())) && (tweet.charAt(ch+1)!=' ' && tweet.charAt(ch+1)!='\t'))
   {
   hash++;
   }
   if(tweet.charAt(ch) == '@' && ((ch+1)<(tweet.length())) && (tweet.charAt(ch+1)!=' ' && tweet.charAt(ch+1)!='\t'))
   {
   attr++;
   }
   if(tweet.charAt(ch) == 'h' && ((ch + 7)<(tweet.length())))
   {
   String a = new String("http://");
   String sub = new String(tweet.substring(ch, ch + 7)); 
     if (sub.equals(a))
     {link++;}
   }



   }

   System.out.println("Number of Hashtags: " + hash);
   System.out.println("Number of Attributions: " + attr);
   System.out.println("Number of Links: " + link);

   }

}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM