Advertisement
Guest User

ArrayAssignment

a guest
Nov 17th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. package javaapplication8;
  2.  
  3. import java.util.Scanner    ;
  4.  
  5. public class JavaApplication8 {
  6.  
  7.    static Scanner sc = new Scanner (System.in)  ;
  8.    
  9.     public static void main(String[] args) {
  10.        
  11.         System.out.print("Enter a sentence: ")  ;
  12.         String input = sc.nextLine() ;
  13.        
  14.         while (input!=null) {
  15.         if (input.isEmpty()) {
  16.             System.out.println("Read Enter Key.");
  17.             break;
  18.         }
  19.  
  20.         else if (sc.hasNextLine()) {
  21.             input = sc.nextLine();
  22.         } else {
  23.             input = null;
  24.         }
  25.         }
  26.  
  27.  
  28.        
  29.        String sentence = input.replaceAll("\\W", "")    ;// removes all special characters and spaces.
  30.        
  31.         sentence = sentence.toUpperCase()   ;
  32.          
  33.  
  34.          
  35.        
  36.        
  37.        
  38.                 int [] frequencies = new int [26]    ;
  39.                 int value = 65  ;
  40.                 double valuecount = 0   ;
  41.                              
  42.                
  43.                
  44.                
  45.                
  46.                 for (int i = 0; i < 26 ; i++) {
  47.                    
  48.                     for (int i2 = 0; i2 < sentence.length(); i2++) {
  49.                         if (sentence.charAt(i2) == value)   {
  50.                             valuecount += 1 ;
  51.                         }
  52.                     }
  53.  
  54.                                 double percent = (valuecount / sentence.length() ) * 100   ;
  55.                         char val = (char) value ;
  56.                         System.out.format (val + " occured %.2f percent of the time. \n", + percent)    ;
  57.                         value += 1 ;
  58.                         valuecount =0  ;
  59.                    
  60.                        
  61.     }
  62.      
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement