forked from dicodingacademy/BelajarJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVokalKonsonan.java
More file actions
49 lines (40 loc) · 1.49 KB
/
Copy pathVokalKonsonan.java
File metadata and controls
49 lines (40 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.dicoding.javafundamental.vokalkonsonan;
import java.util.Scanner;
public class VokalKonsonan {
public static void main(String[] args) {
/*Kamus*/
String word = "";
int vokal = 0;
int konsonan = 0;
/*Program*/
//masukan kalimat
System.out.print("Masukan kalimat : ");
Scanner scanner = new Scanner(System.in);
word = scanner.nextLine();
//memanggil prosedur yang mengembalikan nilai int
vokal = num_vokal(word);
konsonan = num_konsonan(word);
System.out.println("Jumlah huruf vokal : " +vokal);
System.out.println("Jumlah huruf konsonan : " +konsonan);
}
private static int num_vokal(String word) {
int i;
int jumlah_vokal = 0;
for (i = 0; i < word.length(); i++) {
if (word.charAt(i) == 'a' || word.charAt(i) == 'i' || word.charAt(i) == 'u' || word.charAt(i) == 'e' || word.charAt(i) == 'o') {
jumlah_vokal++;
}
}
return jumlah_vokal;
}
private static int num_konsonan(String word) {
int i;
int jumlah_konsonan = 0;
for (i = 0; i < word.length(); i++) {
if (word.charAt(i) != 'a' && word.charAt(i) != 'i' && word.charAt(i) != 'u' && word.charAt(i) != 'e' && word.charAt(i) != 'o' && word.charAt(i) != ' ') {
jumlah_konsonan++;
}
}
return jumlah_konsonan;
}
}