Advertisement
Guest User

Java I Final Project

a guest
Feb 12th, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. // Computes how many times each letter was used in a text provided in a *.txt file
  2. // The program reads in from a file, which has to be save in the same folder as the program itself.
  3. // By default the *.txt file is called - words.txt
  4.  
  5. import java.util.*;
  6. import java.io.*;
  7.  
  8. public class literki {
  9.    
  10.    private static int max = 0;
  11.    private static int whichLetter = 0;
  12.    private static int[] letterCount;
  13.    
  14.    
  15.    public static void main (String[] args) throws FileNotFoundException {
  16.    
  17.    Scanner console = new Scanner(System.in);
  18.    
  19.    String word;
  20.    int temp = 0;
  21.    int h = 0;
  22.    Scanner inFile = new Scanner(new FileReader("words.txt"));
  23.    
  24.    
  25.    letterCount = new int[26];
  26.    
  27.    
  28.    while (inFile.hasNext())
  29.    {
  30.    word = inFile.nextLine();  
  31.    for (int i = 0; i < word.length(); i++)
  32.       {
  33.       temp = word.charAt(i);
  34.       letterCount[temp-97]++;
  35.       }
  36.    }
  37.    max();
  38.    scale();
  39.    for (int j = 0; j< 26; j++)
  40.       {
  41.       h=0;
  42.       System.out.print((char)(j+97) + ":");
  43.      
  44.       for (; h<letterCount[j]; h++)
  45.          {
  46.          System.out.print("* ");
  47.          }
  48.         System.out.print("\n");
  49.       }
  50.  
  51.    }//end main
  52.    
  53.    public static void max ()  
  54.       {
  55.       for (int i = 0; i< 26; i++)
  56.          {
  57.          if (letterCount[i] > max)
  58.             {
  59.             max = letterCount[i];
  60.             whichLetter = i;
  61.             }
  62.          }
  63.       }
  64.    public static void scale ()
  65.       {
  66.       for (int i = 0; i< 26; i++)
  67.          {
  68.          letterCount[i] = 10*letterCount[i]/max;
  69.          }
  70.      
  71.      
  72.       }  
  73.      
  74.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement