Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Computes how many times each letter was used in a text provided in a *.txt file
- // The program reads in from a file, which has to be save in the same folder as the program itself.
- // By default the *.txt file is called - words.txt
- import java.util.*;
- import java.io.*;
- public class literki {
- private static int max = 0;
- private static int whichLetter = 0;
- private static int[] letterCount;
- public static void main (String[] args) throws FileNotFoundException {
- Scanner console = new Scanner(System.in);
- String word;
- int temp = 0;
- int h = 0;
- Scanner inFile = new Scanner(new FileReader("words.txt"));
- letterCount = new int[26];
- while (inFile.hasNext())
- {
- word = inFile.nextLine();
- for (int i = 0; i < word.length(); i++)
- {
- temp = word.charAt(i);
- letterCount[temp-97]++;
- }
- }
- max();
- scale();
- for (int j = 0; j< 26; j++)
- {
- h=0;
- System.out.print((char)(j+97) + ":");
- for (; h<letterCount[j]; h++)
- {
- System.out.print("* ");
- }
- System.out.print("\n");
- }
- }//end main
- public static void max ()
- {
- for (int i = 0; i< 26; i++)
- {
- if (letterCount[i] > max)
- {
- max = letterCount[i];
- whichLetter = i;
- }
- }
- }
- public static void scale ()
- {
- for (int i = 0; i< 26; i++)
- {
- letterCount[i] = 10*letterCount[i]/max;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement