编写代码以使用方法查找字符串中的单词数

时间:2013-06-21 14:47:18

标签: java

我一直在寻找,我找不到任何地方如何使用3种方法来编写字数。这是到目前为止代码的样子。我迷失了如何使用这些方法。我可以不使用不同的方法而只使用一个方法。请帮忙!!!

    public static void main(String[] args) {
    Scanner in = new Scanner (System.in);
    System.out.print("Enter a string: ");
    String s = in.nextLine();
    if (s.length() > 0)
    {
        getInputString(s);
    }
    else 
    {
        System.out.println("ERROR - string must not be empty.");
        System.out.print("Enter a string: ");
        s = in.nextLine();
    }
    // Fill in the body with your code
}

// Given a Scanner, prompt the user for a String.  If the user enters an empty
// String, report an error message and ask for a non-empty String.  Return the
// String to the calling program.
private static String getInputString(String s) {
    int count = getWordCount();
    while (int i = 0; i < s.length(); i++)
    {
        if (s.charAt(i) == " ")
        {
            count ++;
        }
    }
    getWordCount(count);
    // Fill in the body

    // NOTE: Do not declare a Scanner in the body of this method.
}


// Given a String return the number of words in the String.  A word is a sequence of 
// characters with no spaces.  Write this method so that the function call:
//      int count = getWordCount("The quick brown fox jumped");
// results in count having a value of 5.  You will call this method from the main method.
// For this assignment you may assume that
// words will be separated by exactly one space.
private static int getWordCount(String input) {
    // Fill in the body
}

}

编辑: 我已将代码更改为

private static String getInputString(String s) {
    String words = getWordCount(s);
    return words.length();
}


private static int getWordCount(String s) {
    return s.split(" ");
}

但我无法将字符串转换为整数。

10 个答案:

答案 0 :(得分:3)

您已阅读方法的名称,并查看注释以确定方法内应实现的内容以及应返回的值。

getInputString方法签名应为:

private static String getInputString(Scanner s) {

    String inputString = "";

    // read the input string from system in
    // ....

    return inputString;

}

getWordCount方法签名应为:

private static int getWordCount(String input) {

    int wordCount = 0;

    // count the number of words in the input String
    // ...

    return wordCount;
}

main方法应如下所示:

public static void main(String[] args) {

    // instantiate the Scanner variable

    // call the getInputString method to ... you guessed it ... get the input string

    // call the getWordCount method to get the word count

    // Display the word count
}

答案 1 :(得分:2)

                 count=1 //last word must be counted
                 for(int i=0;i<s.length();i++)
                 {
                   ch=s.charAt(i);
                   if(ch==' ')
                   {
                     count++;
                   }
                 }              

答案 2 :(得分:1)

在1-n空白字符上使用trim()split()

private static int getWordCount(String s) {
    return s.trim().split("\\s+").length;
}

调用trim()是必要的,否则如果字符串中有前导空格,您将获得一个额外的“单词”。

将多个空格计为单个单词分隔符需要参数"\\s+"\s是“空白”的正则表达式。 +是“1或更多”的正则表达式。

答案 3 :(得分:1)

您可以通过以下方式执行此操作:

private static int getWordCount(String input) {
    return input.split("\\s+").length;
}

答案 4 :(得分:1)

您需要做的是,计算字符串中的空格数。这是字符串中的单词数。

你会看到你的计数将会减少1,但经过一些思考和追捕,你会发现原因。

快乐学习!

答案 5 :(得分:0)

我不确定你对方法有什么麻烦,但我不认为你需要多个,试试这个:它使用split来分割字符串中的单词,你可以选择分隔符

String sentence = "This is a sentence.";  
String[] words = sentence.split(" ");  

for (String word : words) {  
   System.out.println(word);  
}

然后你可以这样做:

numberOfWords = words.length();

如果您想使用3种方法,可以使用main()方法为您调用方法,例如:

public String getInputString() {

    Scanner in = new Scanner (System.in);
    System.out.print("Enter a string: ");
    String s = in.nextLine();
    if (s.length() > 0) {
        return s;
    } else {
        System.out.println("ERROR - string must not be empty.");
        System.out.print("Enter a string: ");
        return getInputString();
    }
}

public int wordCount(String s) {

    words = splitString(s)

    return words.length();
}

public String[] splitString(String s) {

    return s.split(" ");
}

答案 6 :(得分:0)

使用String.split()方法,例如:

String[] words = s.split("\\s+");
int wordCount = words.length;

答案 7 :(得分:0)

根据您的代码,我认为这是您要做的事情:

private static int getWordCount(String input) {

    int count = 0;

    for (int i = 0; i < input.length(); i++) {
        if (input.charAt(i) == ' ') {
            count++;
        }
    }

    return count;
}

这就是我所做的:

  • 我已将您正在“玩”的代码移到正确的方法中(getWordCount)。
  • 更正了您尝试使用的循环(我认为您有for和while循环混淆)
  • 修正了对空格字符(' '而非" "
  • 的检查

此代码中存在一个错误,您需要了解如何修复:

  • getWordCount("How are you");将在3
  • 时返回2
  • getWordCount("");将返回0
  • getWordCount("Hello");应为1
  • 时,它将返回0
祝你好运!

答案 8 :(得分:0)

最好使用带有参数的split()的简单函数作为空格

int n= str.split(" ").length;

答案 9 :(得分:0)

sh -c