Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA查找单词或短语方法错误:字符串索引超出范围_Java_Syntax Error - Fatal编程技术网

JAVA查找单词或短语方法错误:字符串索引超出范围

JAVA查找单词或短语方法错误:字符串索引超出范围,java,syntax-error,Java,Syntax Error,我试图创建一个findText()方法来查找字符串中的单词或短语 static String usrStr = "There was was once a boy named Timmy. Timmy liked to play in the park."; public static void findText() { int matchCntr = 0; int numInstances = 0; String phrase = ""; System.out.printl

我试图创建一个findText()方法来查找字符串中的单词或短语

static String usrStr = "There was was once a boy named Timmy. Timmy liked to play in the park.";

public static void findText() {
  int matchCntr = 0;
  int numInstances = 0;
  String phrase = "";

  System.out.println("Enter a word or phrase to be found: ");
  phrase = scnr.nextLine();

  for (int i = 0; i < usrStr.length(); i++) {
     if (usrStr.charAt(i) == phrase.charAt(matchCntr)) { //FIX ME
        matchCntr++;
        if (matchCntr == phrase.length()) {
           numInstances++;
           matchCntr = 0;
        }
     }
     else {
        matchCntr = 0;
     }
  }

  System.out.println("\"" + phrase + "\" instances: " + numInstances);

  return;

我读到这个错误可能是因为字符串“短语”至少不是38个字符,但这仍然不能帮助我修复代码。对于如何修复这段代码以使其按预期工作,同时避免StringIndexOutOfBoundsException错误,您有什么解决方案吗?

我使用了一个缓冲读取器,必须初始化I。有一些函数可以为您实现这一点

String usrStr = "There was was once a boy named Timmy. Timmy liked to play in the park.";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int matchCntr = 0;
int numInstances = 0;
String phrase = "";
System.out.println("Enter a word or phrase to be found: ");
phrase = br.readLine();

for (int i = 0; i < usrStr.length(); i++)
{
  if (usrStr.charAt(i) == phrase.charAt(matchCntr))
  {  //FIX ME
     matchCntr++;
     if (matchCntr == phrase.length())
     {
       numInstances++;
       matchCntr = 0;
     }
  }
  else
  {
    matchCntr = 0;
  }
}
System.out.println("\"" + phrase.toString() + "\" instances: " + numInstances);
String usrStr=“从前有个男孩叫蒂米。蒂米喜欢在公园里玩。”;
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
int matchCntr=0;
int numInstances=0;
字符串短语=”;
System.out.println(“输入要查找的单词或短语:”);
短语=br.readLine();
对于(int i=0;i
我在运行时没有收到任何错误。在`for(i=0;ii的初始化在哪里?这是一个更大程序的一部分,我忘了包含初始化,但在使用该方法之前它应该初始化为静态int。谢谢我尝试在虚拟机中运行此操作是否有区别?可能重复的
短语似乎为空,因此请检查重复的问题。使用Scanner类和BufferedReader之间有什么区别?老实说,我无法给您一个好的答案。我过去在使用扫描仪时遇到过问题,我发现一个缓冲读取器似乎最适合我。查看此链接。。。它应该更好地解释这些差异。
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 0
String usrStr = "There was was once a boy named Timmy. Timmy liked to play in the park.";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int matchCntr = 0;
int numInstances = 0;
String phrase = "";
System.out.println("Enter a word or phrase to be found: ");
phrase = br.readLine();

for (int i = 0; i < usrStr.length(); i++)
{
  if (usrStr.charAt(i) == phrase.charAt(matchCntr))
  {  //FIX ME
     matchCntr++;
     if (matchCntr == phrase.length())
     {
       numInstances++;
       matchCntr = 0;
     }
  }
  else
  {
    matchCntr = 0;
  }
}
System.out.println("\"" + phrase.toString() + "\" instances: " + numInstances);