Advertisement
hellom38

Example3 Java

Aug 17th, 2022 (edited)
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8.  
  9.  
  10. public class FreqTableExampleOriginal {
  11.  
  12.     public static final int NUM_ASCII_CHAR = 128;
  13.  
  14.     // program to create a frequency table.
  15.     // Example of simple try catch blocks to deal with checked exceptions
  16.     public static void main(String[] args)
  17.         {
  18.  
  19.         int[] freqs = createFreqTableURL("http://www.utexas.edu/");
  20.  
  21.         if( freqs.length == 0)
  22.             System.out.println("No frequency table created due to problems when reading from file");
  23.         else{
  24.             for(int i = 0; i < NUM_ASCII_CHAR; i++){
  25.                 System.out.println("charcater code: " + i + " ,character: " + (char)i + " ,frequency: " + freqs[i]);
  26.             }
  27.             System.out.println("Total characters in file: " + sum(freqs));
  28.         }
  29.  
  30.  
  31.         freqs = new int[]{};
  32.         try{
  33.             freqs = createTable("ciaFactBook2008.txt");
  34.         }
  35.         catch(FileNotFoundException e){
  36.             System.out.println("File not found. Unable to create freq table" + e);
  37.         }
  38.         catch(IOException e){
  39.             System.out.println("Problem while reading from file. Unable to create freq table" + e);
  40.         }
  41.         if( freqs.length == 0)
  42.             System.out.println("No frequency table created due to problems when reading from file");
  43.         else{
  44.             for(int i = 0; i < freqs.length; i++){
  45.                 System.out.println("charcater code: " + i + " ,character: " + (char)i + " ,frequency: " + freqs[i]);
  46.             }
  47.             System.out.println("Total characters in file: " + sum(freqs));
  48.         }
  49.  
  50.     }
  51.  
  52.  
  53.     // return sum of ints in list
  54.     // list may not be null
  55.     private static int sum(int[] list) {
  56.         assert list != null : "Failed precondition, sum: parameter list" +
  57.             " may not be null.";
  58.         int total = 0;
  59.         for(int x : list){
  60.             total += x;
  61.         }
  62.         return total;
  63.     }
  64.  
  65.  
  66.     // pre: url != null
  67.     // Connect to the URL specified by the String url.
  68.     // Map characters to index in array.
  69.     // All non ASCII character dumped into one element of array
  70.     // If IOException occurs message printed and array of
  71.     // length 0 returned.
  72.     public static int[] createFreqTableURL (String url){
  73.         if(url == null)
  74.             throw new IllegalArgumentException("Violation of precondition. parameter url must not be null.");
  75.  
  76.         int[] freqs = new int[NUM_ASCII_CHAR];
  77.         try {
  78.         URL inputURL = new URL(url);
  79.         InputStreamReader in
  80.             = new InputStreamReader(inputURL.openStream());
  81.  
  82.         while(in.ready()){
  83.             int c = in.read();
  84.             if(0 <= c && c < freqs.length)
  85.                 freqs[c]++;
  86.             else
  87.                 System.out.println("Non ASCII char: " + c + " " + (char) c);
  88.         }
  89.         in.close();
  90.         }
  91.         catch(MalformedURLException e){
  92.             System.out.println("Bad URL.");
  93.             freqs = new int[0];
  94.         }
  95.         catch(IOException e){
  96.             System.out.println("Unable to read from resource." + e);
  97.             freqs = new int[0];
  98.         }
  99.         return freqs;
  100.     }
  101.  
  102.  
  103.  
  104.     // Connect to the file specified by the String fileName.
  105.     // Assumes it is in same directory as compiled code.
  106.     // Map characters to index in array.
  107.     public static int[] createTable(String fileName) throws FileNotFoundException, IOException{
  108.         int[] freqs = new int[NUM_ASCII_CHAR];
  109.         File f = new File(fileName);
  110.         FileReader r = new FileReader(f);
  111.         while( r.ready() ){
  112.             int ch = r.read();
  113. //            if( 0 <= ch && ch <= NUM_ASCII_CHAR)
  114. //                freqs[ch]++;
  115. //            else
  116. //                freqs[INDEX_NON_ASCII]++;
  117.             if(0 <= ch && ch < freqs.length)
  118.                 freqs[ch]++;
  119.             else
  120.                 System.out.println((char) ch);
  121.                
  122.         }
  123.         r.close();
  124.         return freqs;
  125.     }
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement