모음 문자로 시작하는 단어를 찾으려면 -
-
String 클래스의 split() 메서드 String 클래스의 split() 메서드를 사용하여 주어진 문자열을 String 배열로 분할합니다.
-
for 루프에서 얻은 배열의 각 단어를 통과합니다.
-
charAt() 메서드를 사용하여 얻은 배열에서 각 단어의 첫 번째 문자를 가져옵니다.
-
if 루프를 사용하여 문자가 모음 중 하나와 같은지 확인하면 단어가 인쇄됩니다.
예시
다음 내용이 포함된 텍스트 파일이 있다고 가정합니다. -
Tutorials Point originated from the idea that there exists a class of readers who respond better to on-line content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.
다음 Java 프로그램은 이 파일에서 모음 문자로 시작하는 모든 단어를 인쇄합니다.
import java.io.File;
import java.util.Scanner;
public class WordsStartWithVowel {
public static String fileToString(String filePath) throws Exception {
Scanner sc = new Scanner(new File(filePath));
StringBuffer sb = new StringBuffer();
String input = new String();
while (sc.hasNextLine()) {
input = sc.nextLine();
sb.append(input);
}
return sb.toString();
}
public static void main(String args[]) throws Exception {
String str = fileToString("D:\\sample.txt");
String words[] = str.split(" ");
for(int i = 0; i < words.length; i++) {
char ch = words[i].charAt(0);
if(ch == 'a'|| ch == 'e'|| ch == 'i' ||ch == 'o' ||ch == 'u'||ch == ' ') {
System.out.println(words[i]);
}
}
}
} 출력
originated idea exists a of on-line and at own of