Selasa, 27 Mei 2014

Menghitung Frekuensi data yang diinput Java Netbeans



package frekuensidata;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String temp = "";
        System.out.print("Masukkan kata : ");
        String input = sc.nextLine().toLowerCase();
        for(int i = 0; i < input.length(); i++){
            boolean exist = false;
            for (int j = 0; j < temp.length(); j++){
                if(input.charAt(i)==temp.charAt(j)){
                    exist = true;
                }
            }
            if(!exist){
                temp = temp + input.charAt(i);
            }
        }
        System.out.println();
        System.out.println("Himpunan huruf dari inputan : "+temp);
        System.out.println();
        System.out.println("Frekuensi  huruf :");
        for (int i = 0; i < temp.length(); i++){
            int sum = 0;
            for(int j = 0; j < input.length(); j++){
                if(temp.charAt(i)==input.charAt(j)) sum++;
            }
            if(temp.charAt(i)==' ') System.out.println(" = "+sum);
            else System.out.println(temp.charAt(i)+" = "+sum);
}
}
}

Tidak ada komentar: