-1

I am trying to create a findText() method to find a word or phrase in a String.

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;

If it worked correctly I should get this ouput:

Enter a word or phrase to be entered: <Timmy>
"Timmy" instances: 2

But when I run this code I get this error. I marked where the error occurs in the main body of code with a //FIX ME.

Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 0

I read that this error is probably because the string "phrase" is not at least 38 characters, but that still doesn't help me fix the code. Do you have any solutions for how to fix this piece of code to work as intended while avoiding the StringIndexOutOfBoundsException error?

  • I did not get any error while running. Where is the initialization of `i` in ` for (i = 0; i < usrStr.length(); i++)` ? – Raghav Nov 06 '16 at 23:37
  • This is part of a bigger program, I forgot to include the initialization, but it should have been initialized as a static int before the method. Thanks! – D. Snowhawk Nov 06 '16 at 23:46
  • does it make a difference that I am trying to run this in a virtual machine? – D. Snowhawk Nov 06 '16 at 23:51
  • Possible duplicate of [Scanner issue when using nextLine after nextXXX](http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx) – Tom Nov 06 '16 at 23:53
  • `phrase` seems to be empty, so check the duplicate question. – Tom Nov 06 '16 at 23:53
  • 'phrase' should be input by the user – D. Snowhawk Nov 07 '16 at 00:02
  • Read the dupe and then look at your code ... since you omitted important parts, there is nothing we can do here now. – Tom Nov 07 '16 at 00:10

1 Answers1

0

I used a buffered reader and had to initialize i. There are functions out there that will do this for you.

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);
JohnG
  • 9,259
  • 2
  • 20
  • 29
  • what is the difference between using the Scanner class and BufferedReader? – D. Snowhawk Nov 07 '16 at 00:01
  • Honestly I could not give you a good answer. I have had problems using the scanner in the past and found a buffered reader seems to work best for me. Check out this link... it should give a better explanation of the differences. [Scanner vs. BufferedReader](http://stackoverflow.com/questions/2231369/scanner-vs-bufferedreader) – JohnG Nov 07 '16 at 00:07