我目前正在做一個練習(在任何人發出之前不做作業),我被困在問題的最後部分。Java字符串輸出字符串限制範圍到字母表
的問題是:
Write a program which will input a String from the keyboard, output the number of
seperate words, where a word is one or more characters seperated by spaces. Your
program should only count words as groups of characters in the rang A..Z and a..z
我可以做的第一部分沒有問題,你可以通過我的代碼中看到:
進口java.util.Scanner的;
public class Exercise10 {
public static void main(String[] args) {
String input;
int counter = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your text: ");
input = keyboard.nextLine();
for(int i = 0; i < input.length(); i++){
if(input.charAt(i) == ' '){
counter++;
}
}
System.out.println(counter + 1);
keyboard.close();
}
}
當然,這是困惑我的部分是這樣的:
Your program should only count words as groups of characters in the rang A..Z and
a..z
我應該在這種情況下怎麼辦?
你需要查找的正則表達式 – tom
你做我的方式t是錯的。計數空格與計數單詞不同。如果用戶將''字「'? –
Pshemo
還沒有學過正則表達式,所以我寧可不使用,直到我們被教會了,因爲我不想迷惑自己。 – user2830571