Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mycompany.week9lab;
- import java.util.Scanner;
- public class MainClass {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- // Get input string
- System.out.println("enter a phrase");
- String input = sc.nextLine().toLowerCase().replaceAll(" ", "");
- int lettersTotal = input.length();
- System.out.println("Letter\tFrequency\tpercentage");
- while (input.length() > 0) {
- // Get first letter as string
- char letter = input.charAt(0);
- int frequency = 0;
- String inputStripped = "";
- for (int i=0;i<input.length();i++) {
- if (input.charAt(i) == letter) {
- // Letter match, add to frequency
- frequency++;
- } else {
- // Not a match, add letter to inputStripped
- inputStripped += input.charAt(i);
- }
- }
- // Calculate percentage
- double percentage = Math.round(((double) frequency/lettersTotal)*100);
- System.out.println(letter + "\t" + frequency + "\t\t" + percentage);
- input = inputStripped;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement