Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication8;
- import java.util.Scanner;
- public class JavaApplication8 {
- static Scanner sc = new Scanner (System.in);
- public static void main(String[] args) {
- System.out.print("Enter a sentence: ");
- System.out.println ("");
- //Code works completely fine other than the counter not accumulating the first line of words.
- String accum = new String() ;
- String input = sc.nextLine() ;
- while (input!=null) {
- if (input.isEmpty()){
- System.out.println("Read Enter Key.");
- break;
- } else if (sc.hasNextLine()){
- input = sc.nextLine();
- } else {
- input = null;
- }
- accum += input;
- }
- String sentence = accum.replaceAll("\\W", "");
- sentence = sentence.toUpperCase();
- int [] frequencies = new int [26];
- int value = 65;
- double valuecount = 0;
- for (int i = 0; i < 26 ; i++) {
- for (int i2 = 0; i2 < sentence.length(); i2++){
- if (sentence.charAt(i2) == value){
- valuecount += 1 ;
- }
- }
- double percent = (valuecount / sentence.length() ) * 100;
- char val = (char) value;
- System.out.format (val + " occured %.2f percent of the time. \n", + percent);
- value += 1;
- valuecount =0 ;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement