ChickenCounter Program和ChickenCounter Tester with Java

时间:2017-02-27 03:13:46

标签: java

// The countChicken() method should count the number of occurrences of the word chicken (or some other word) in the string passed to it.
// Use recursion to accomplish this (countChicken() should call itself whenever "chicken" is found).
// Make this class flexible by passing the word you want to search for as a parameter to the constructor.  If
// nothing is passed to the constructor (there is no parameter), then the search word should be "chicken".
import static java.lang.System.*;

public class ChickenCounter
{
   //Create two constructor methods here, one with no parameter and one with one parameter
    private String word;
    private int length;
    private int x;
    public ChickenCounter()
{
  word = "chicken";
  length = 7;
  x = 0;
}
public ChickenCounter(String z)
{
 word = z;
 length = word.length();
 x = 0;
}
// The parameter bigString below should contain the long string that you want to find the word chicken in
public int countChickens(String bigString)   
{
    if(bigString.length() <= length)
    {
        if(bigString.equals(word))
        {
            return 1;
        }

        else
        {
            String temp = bigString.substring(x, length-1);//line that is highlighted when given the error 
            if(temp.equals(word))
            {
                bigString = bigString.substring(x, bigString.indexOf(word)) + bigString.substring(bigString.indexOf(word)); //dont know if this is the correct syntax of the method indexOf()
                return countChickens(bigString) + 1;
            }
            else
            {
                x++;
                return countChickens(bigString); //this line is also part of the problem
            }
        }
    }
      return 0;
   }
}


import static java.lang.System.*;


public class ChickenCounterTester
{
    public static void main(String args[])
    {
        ChickenCounter counter = new ChickenCounter();

    System.out.println(counter.countChickens("itatfun")); //0 <--what it supposed to output

    System.out.println(counter.countChickens("itatchickenfun")); //1

    System.out.println(counter.countChickens("chchickchickenenicken")); //3

    System.out.println(counter.countChickens("chickchickfun")); //0

    System.out.println(counter.countChickens("chickenbouncetheballchicken")); //2       


    //pass the word to search for to the object

     counter = new ChickenCounter("java");

    System.out.println("");

    System.out.println(counter.countChickens("jjajavavaavaisfun")); //3 

    System.out.println(counter.countChickens("I want some jajavava")); //2

}

}

输出继电器: 0 1 2 0 7

1 2

我继续得到索引超出范围的错误,表示超出范围-1。该程序试图找到给我的字符串中的某个单词。我的老师正在使用这个词作为此代码的示例。他想通过每行代码并找到单词chicken如果我们发现它将它添加到计数器变量中,如果不删除它并再次通过该字符串并尝试再次找到该单词。

2 个答案:

答案 0 :(得分:0)

您的教授希望您使用递归来计算字符串中单词出现次数。您当前的countChickens方法不包含正确的逻辑。它应该类似于以下内容:

public int countChickens(String bigString){
      if(bigString.contains(word)){
        return 1 + countChickens(
         bigString.substring(
          bigString.indexOf(word)+word.length()));

      }else{
       return 0;
     }
  }

答案 1 :(得分:0)

counter.countChickens("chchickchickenenicken")   

对于此输入,如何获得3作为输出。在此输入中,只有一个字可用。这就是你没有得到正确输出的原因。否则 @Eduardo Dennis countChickens方法的工作正确。