SHARE
TWEET

Array Assignment

a guest Nov 18th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         System.out.print("Enter a sentence: ");
  11.         System.out.println ("");
  12.        
  13.        
  14.         //Code works completely fine other than the counter not accumulating the first line of words.
  15.      
  16.        
  17.         String accum = new String()   ;
  18.        
  19.         String input = sc.nextLine() ;
  20.                  
  21.                
  22.         while (input!=null) {
  23.             if (input.isEmpty()){
  24.                 System.out.println("Read Enter Key.");
  25.                 break;
  26.             } else if (sc.hasNextLine()){
  27.                 input = sc.nextLine();
  28.             } else {
  29.                 input = null;
  30.             }
  31.         accum += input;
  32.         }
  33.        
  34.         String sentence = accum.replaceAll("\\W", "");
  35.         sentence = sentence.toUpperCase();
  36.        
  37.         int [] frequencies = new int [26];
  38.         int value = 65;
  39.         double valuecount = 0;
  40.  
  41.         for (int i = 0; i < 26 ; i++) {
  42.             for (int i2 = 0; i2 < sentence.length(); i2++){
  43.                 if (sentence.charAt(i2) == value){
  44.                     valuecount += 1 ;
  45.                 }
  46.             }
  47.             double percent = (valuecount / sentence.length() ) * 100;
  48.             char val = (char) value;
  49.             System.out.format (val + " occured %.2f percent of the time. \n", + percent);
  50.             value += 1;
  51.             valuecount =0 ;
  52.         }
  53.     }
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top