2017-10-03 15 views
0

これは私が今までに持っているコードです。私がしたいのは、作成したメソッドを呼び出して、ユーザーの入力にいくつの単語があるかを表示することです。私が何を呼んでいるのが間違っているのか、それとも何をしているのか分かっている人から何か助けを得ることができるのかどうかは分かりません。文字列から単語を数え、メソッドから呼び出しますか?

import java.util.Scanner; 

public class Chapter3Question5 { 

    public static void main(String[] args) { 
     Scanner in = new Scanner(System.in); 

     System.out.println("Type some words: "); 
     String s = in.nextLine(); 
     System.out.println("There are " + (countWords) + " words."); 
     //want to call the method here and print the results 

     in.close(); 
    } 

    public static int countWords(String str) { 
     int wordCount = 0; 

     boolean word = false; 
     int endLine = str.length() - 1; 

     for (int i = 0; i < str.length(); i++) { 

      if (Character.isLetter(str.charAt(i)) && i != endLine) { 
       word = true; 
      } else if (!Character.isLetter(str.charAt(i)) && word) { 
       wordCount++; 
       word = false; 
      } else if (Character.isLetter(str.charAt(i)) && i == endLine) { 
       wordCount++; 
      } 
     } 
     return wordCount; 
    } 
} 
+0

洞察を意味すると思いますか? –

答えて

3

あなたは正しくメソッドを呼び出していませんでした:

System.out.println("There are " + (countWords) + " words."); 

System.out.println("There are " + countWords(s) + " words."); 

になるべき、あなたはまた、スペースや使用上の文字列を分割して同じことをしようとして行うことができます出力された配列のlength()メソッドこれは、あなたの方法をずっと複雑にしません。

azroの答えを参照してください。

4

あなたは、あなたの場合には、そのnameと良いparametersしてメソッドを呼び出す必要があります:それから

String s = in.nextLine(); 
System.out.println("There are " + countWords(s) + " words."); 

一部を、あなたの方法は良いですが、それは


ヒントを働いています(ちょうど別の方法を知らせるために)これは同じことを行います:

public static int countWords(String str) { 
    return str.split("\\s+").length; 
} 

スペースがあるところで文を分割し、配列(単語の配列so)を作成し、長さを取る:単語数

1

あなたのメソッドを適切に呼び出す必要があります。それはパラメータを期待し、あなたは何も渡していません。また、メソッド呼び出しにかっこもありません。だから、それは次のようになります。

public class Chapter3Question5 { 

    public static void main(String[] args) { 
     Scanner in = new Scanner(System.in); 

     System.out.println("Type some words: "); 
     String s = in.nextLine(); 
     System.out.println("There are " + (countWords(s)) + " words."); 
     //want to call the method here and print the results 

     in.close(); 
    } 

    public static int countWords(String str) { 
     return str == null || str.length() == 0 ? 0 : str.split("\\s+").length; 
    } 
} 

ここで私はの配列で、その結果、スペースで文字列を分割しています:サイドノートとして

System.out.println("There are " + (countWords(s)) + " words."); // add parentheses and parameters 

を、その機能は次のように簡単に実装することができます言葉。次に、配列の長さを返します。

テストおよび出力:

タイプいくつかの単語:
はいはいのHello Worldさようなら
は4つの言葉があります。

+0

@JaviGお読みください:[私の質問に誰かが答えたときにどうすればいいですか?](https://stackoverflow.com/help/someone-answers) –

2

私はあなたが何に

import java.util.Scanner; 

public class Chapter3Question5 { 

public static void main(String[] args) { 
Scanner in = new Scanner(System.in); 

System.out.println("Type some words: "); 
String s = in.nextLine(); 
System.out.println("There are " + countWords(s) + " words."); 
//want to call the method here and print the results 

in.close(); 
} 
public static int countWords(String str) 
{ 
int wordCount = 0; 

    boolean word = false; 
    int endLine = str.length() - 1; 

    for (int i = 0; i < str.length(); i++) { 

     if (Character.isLetter(str.charAt(i)) && i != endLine) { 
      wordCount++; 
      word = true; 

     } else if (!Character.isLetter(str.charAt(i)) && word) { 
      wordCount++; 
      word = false; 

     } else if (Character.isLetter(str.charAt(i)) && i == endLine) { 
      wordCount++; 
     } 
    } 
    return wordCount; 
} 
} 
関連する問題