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