Java 字数超出范围的错误

Java 字数超出范围的错误,java,Java,我正在尝试编写自己的Java字数计算程序。我知道可能已经有了一种方法,但我想让它发挥作用。我在第14行遇到越界错误。我试图使用一个输入字来计算它在输入字符串中出现的次数。所以我循环到stringlength-wordlength,但这就是问题所在 代码如下: import java.util.Scanner; public class wordcount { public static void main(String[] args) { Scanner s = new S

我正在尝试编写自己的Java字数计算程序。我知道可能已经有了一种方法,但我想让它发挥作用。我在第14行遇到越界错误。我试图使用一个输入字来计算它在输入字符串中出现的次数。所以我循环到
stringlength
-
wordlength
,但这就是问题所在

代码如下:

import java.util.Scanner;

public class wordcount {

  public static void main(String[] args)
  { 
    Scanner s = new Scanner(System.in);
    System.out.print( "Enter word : "  );
    String word = s.nextLine();
    Scanner t = new Scanner(System.in);
    System.out.print("Enter string: ");
    String string = t.nextLine();
    int count = 0;
    for (int i = 0; i < string.length()-word.length(); i = i+1){
      String substring = string.substring(i,i+word.length());
      if (match(substring, word)==true){
        count += 1;
      }
    }

    System.out.println("There are "+count+ " repetitions of the word "+word);

  }

  public static boolean match(String string1, String string2){
      for (int i=0; i<string1.length(); i+=1){
          if (string1.charAt(i)!=string2.charAt(i)){
            return false;
          }             
      }
      return true;
  }
}
import java.util.Scanner;
公共类字数{
公共静态void main(字符串[]args)
{ 
扫描仪s=新的扫描仪(System.in);
系统输出打印(“输入单词:”);
String word=s.nextLine();
扫描器t=新扫描器(System.in);
System.out.print(“输入字符串:”);
String String=t.nextLine();
整数计数=0;
for(int i=0;i对于(inti=0;i我没有得到越界错误,您能告诉我您对单词和字符串使用了什么值吗

我发现您的程序有一个bug。如果word等于string,它仍然返回计数0。我建议再添加一次迭代,改用regionMatches。regionMatches会使您的匹配方法过时,如果word.length()+I等于或大于string.length(),它将返回false,从而避免越界问题

正如您所见,我还将计算转移到了一个单独的方法,这将使您的代码更具可读性和可测试性

正如Christian所指出的,您确实只需要一个扫描对象。我已经修改了下面的代码来反映它

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter word : ");
    String word = sc.nextLine();
    System.out.print("Enter string: ");
    String string = sc.nextLine();
    int count = calculateWordCount(word, string);
    System.out.println("There are " + count + " repetitions of the word " + word);
}

private static int calculateWordCount(String word, String string) {
    int count = 0;
    for (int i = 0; i < string.length() - word.length() + 1; i++) {
        if (word.regionMatches(0, string, i, word.length())) {
            count++;
        }
    }
    return count;
}
publicstaticvoidmain(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
系统输出打印(“输入单词:”);
字符串字=sc.nextLine();
System.out.print(“输入字符串:”);
String String=sc.nextLine();
int count=calculateWordCount(字、字符串);
System.out.println(“单词“+单词”有“+计数+”重复);
}
私有静态int calculateWordCount(字符串字、字符串){
整数计数=0;
for(int i=0;i
首先,不需要两个
扫描仪
s,您可以使用同一个
扫描仪
对象进行许多输入

此外,如果出现这种情况,则

if (match(substring, word) == true)
可以像这样重写

if (math(substring, word))
我还建议您使用
I++
来增加循环变量。这不是严格必要的,但“几乎”是一种惯例。您可以

现在,关于
IndexOutOfBoundsException
,我已经测试了代码,没有找到任何输入示例来获取它

此外,还有一个问题,您在
中缺少一次迭代:

for (int i = 0; i < string.length() - word.length() + 1; i++) { // Add  '+ 1'
    String substring = string.substring(i, i + word.length());
    // System.out.println(substring);
    if (match(substring, word)) {
        count++;
    }
}
for(inti=0;i

您可以通过在循环中放入一个print语句来测试它,以打印每个子字符串。

它似乎工作正常。您使用什么输入来获得IndexOutOfBoundsException?谢谢,这已经解决了它。