2013-06-21 27 views
2

我一直在寻找,我找不到任何地方如何使用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(" "); 
} 

但我不能得到字符串转换为整数。

+1

看起来像你使用罚款,你用的方法有什么问题的方法呢? – Stephan

+2

可以在这里提出具体的问题,但我们不会做你的功课。请在您尝试过的方式以及卡住的地方非常清楚。 –

+1

'if(s.charAt(i)==“”)'将始终为假,因为您在询问char是否与String相同。 ''''代表空格作为字符 – Edd

回答

0

我不知道你有什么麻烦,但我不认为你需要超过一个,试试这个:它使用拆分拆分字符串中的单词,并且您可以选择的定界符

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(" "); 
} 
+0

我可以做到这一点,但我必须使用3. – user2509405

+0

umm,在这种情况下,您可以有一种方法接受字符串,该字符串传递给此处列出的wordcount方法,该方法调用将字符串拆分的方法 – Stephan

+0

@ user2509405你去了,我为你做了3个方法 – Stephan

0

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

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

使用trim()split() 1-N空格字符:

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

trim()的调用是必要的,否则你会得到一个额外的“字“如果字符串中有前导空格。

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

+0

首先,这不能编译。其次,他应该学习一些东西。 –

+0

@RosdiKasim哎呀!我复制了错误的方法签名(固定)。此外,他现在是否在学习一些东西,我已经添加了一些解释? – Bohemian

+0

@RosdiKasim:他已经尝试了一些东西,这就是我们回答 –

1

你可以这样做:

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

我试图将字符串从第一个方法移动到getIntputString。它给了我一个不适用的错误,它的构建正确。然后让int去getWordCount的方法。我无法让代码转向下一个方法。 – user2509405

+1

我可能会建议'“\\ s +”'而不是''“'。简单地分割每个'\ u0020'几乎肯定会错误地评论这个评论中的单词,例如,因为我通常在句点之后使用两个空格。但总的想法很好。 – cHao

+0

[@ user2509405](http://stackoverflow.com/users/2509405/user2509405)可以复制过去的确切错误 – user2509423

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

什么你需要做的是,统计字符串中的空格数。这是字符串中的单词数量。

你会看到你的计数会减1,但经过一番思考和错误狩猎后,你会找出原因。

快乐学习!

+1

糟糕。如果您查看源代码,此评论有二十三个空格。但只有二十一个字。并非每个空间都会分开单词。 – cHao

+0

@cHao,你读过他的'getWordCount'方法之上的评论吗? **对于这项任务,你可能会认为单词会被一个空格隔开。** –

+0

实际上,我没有。我倾向于下意识地过滤评论;除非他们指出,否则他们对我来说都是看不见的,或者他们太过分了,或者很滑稽。 – cHao

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)。
  • 更正你试图使用循环(我认为你有和while循环混淆)
  • 修正你的空格字符检查(' '" "

有一个在这个代码中的bug你将需要制定出如何解决:

  • getWordCount("How are you");将回到2时,它应该是3
  • getWordCount("");将返回0
  • getWordCount("Hello");将返回0,当它应该是1

祝你好运!

+0

似乎在问题中提供的代码已被修改,因为我发布了这个 – Edd

+0

是啊...出于某种原因,实际上可以算出单词的代码已被删除。我正在考虑恢复。 – cHao

0

更好地利用溢出的()带参数的简单功能空间

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

一个解释会很好,它也是与op发布的相同的代码。 –

0
public static int Repeat_Words(String arg1,String arg2) 
{ 
    //It find number of words can be formed from a given string 

    if(arg1.length() < 1 || arg2.length() < 1) 
     return 0; 

    int no_words = 99999; 
    char[] str1 = arg1.toCharArray(); 
    char[] str2 = arg2.toCharArray(); 


    for(int x = 0; x < str1.length; x++) 
    { 
     int temp = 0; 
     for(int y = 0; y < str2.length; y++) 
     { 
      if(str1[x] == str2[y]) 
      temp++; 
     } 
     if(temp == 0) 
      return 0; 
     if(no_words > temp) 
      no_words = temp; 

     temp = 0; 
    } 

    return no_words; 
} 
相关问题