0

I'm trying to write my own Java word count program. I know there may already be a method for this, but I'd like to get it work. I'm getting an out of bounds error at line 14. I'm trying to use an input word to count how many times it appears in an input string. So I'm looping up to stringlength - wordlength, but that's where the problem is.

Here is the code:

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;
  }
}
ztirom
  • 4,382
  • 3
  • 28
  • 39
vap
  • 151
  • 1
  • 2
  • 9

2 Answers2

0

I'm not getting an out of bounds error, can you tell me what values you were using for word and string?

I have identified a bug with your program. If word is equal to string, it still returns count 0. I suggest adding one more iteration and using regionMatches instead. RegionMatches makes your match method obsolete and will return false if word.length() + i is equal or greater than string.length(), avoiding out of bounds issues.

As you can see I also moved the calculations to a seperate method, this will make your code more readable and testable.

And as Christian pointed out; you indeed do only need one Scanner object. I've adapted the code below to reflect it.

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;
}
Reinstate Monica
  • 2,767
  • 3
  • 31
  • 40
0

First of all, two Scanners are not necessary, you can do many inputs with the same Scanner object.

Also, this if condition

if (match(substring, word) == true)

can be rewritten like

if (math(substring, word))

I would also recommend you to use i++ to increase the loop variable. Is not strictly necessary but is "almost" a convention. You can read more about that here.

Now, about theIndexOutOfBoundsException, I've tested the code and I don't find any input samples to get it.

Besides, there is an issue, you are missing one iteration in the for:

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++;
    }
}

You can test it by putting a print statement inside the loop, to print each substring.

Community
  • 1
  • 1
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73