Examples of smaller Java programs that do various interesting things.

FreqTableExampleOriginal.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. public class FreqTableExampleOriginal {
  9. public static final int NUM_ASCII_CHAR = 128;
  10. // program to create a frequency table.
  11. // Example of simple try catch blocks to deal with checked exceptions
  12. public static void main(String[] args)
  13. {
  14. int[] freqs = createFreqTableURL("http://www.utexas.edu/");
  15. if( freqs.length == 0)
  16. System.out.println("No frequency table created due to problems when reading from file");
  17. else{
  18. for(int i = 0; i < NUM_ASCII_CHAR; i++){
  19. System.out.println("charcater code: " + i + " ,character: " + (char)i + " ,frequency: " + freqs[i]);
  20. }
  21. System.out.println("Total characters in file: " + sum(freqs));
  22. }
  23. freqs = new int[]{};
  24. try{
  25. freqs = createTable("ciaFactBook2008.txt");
  26. }
  27. catch(FileNotFoundException e){
  28. System.out.println("File not found. Unable to create freq table" + e);
  29. }
  30. catch(IOException e){
  31. System.out.println("Problem while reading from file. Unable to create freq table" + e);
  32. }
  33. if( freqs.length == 0)
  34. System.out.println("No frequency table created due to problems when reading from file");
  35. else{
  36. for(int i = 0; i < freqs.length; i++){
  37. System.out.println("charcater code: " + i + " ,character: " + (char)i + " ,frequency: " + freqs[i]);
  38. }
  39. System.out.println("Total characters in file: " + sum(freqs));
  40. }
  41. }
  42. // return sum of ints in list
  43. // list may not be null
  44. private static int sum(int[] list) {
  45. assert list != null : "Failed precondition, sum: parameter list" +
  46. " may not be null.";
  47. int total = 0;
  48. for(int x : list){
  49. total += x;
  50. }
  51. return total;
  52. }
  53. // pre: url != null
  54. // Connect to the URL specified by the String url.
  55. // Map characters to index in array.
  56. // All non ASCII character dumped into one element of array
  57. // If IOException occurs message printed and array of
  58. // length 0 returned.
  59. public static int[] createFreqTableURL (String url){
  60. if(url == null)
  61. throw new IllegalArgumentException("Violation of precondition. parameter url must not be null.");
  62. int[] freqs = new int[NUM_ASCII_CHAR];
  63. try {
  64. URL inputURL = new URL(url);
  65. InputStreamReader in
  66. = new InputStreamReader(inputURL.openStream());
  67. while(in.ready()){
  68. int c = in.read();
  69. if(0 <= c && c < freqs.length)
  70. freqs[c]++;
  71. else
  72. System.out.println("Non ASCII char: " + c + " " + (char) c);
  73. }
  74. in.close();
  75. }
  76. catch(MalformedURLException e){
  77. System.out.println("Bad URL.");
  78. freqs = new int[0];
  79. }
  80. catch(IOException e){
  81. System.out.println("Unable to read from resource." + e);
  82. freqs = new int[0];
  83. }
  84. return freqs;
  85. }
  86. // Connect to the file specified by the String fileName.
  87. // Assumes it is in same directory as compiled code.
  88. // Map characters to index in array.
  89. public static int[] createTable(String fileName) throws FileNotFoundException, IOException{
  90. int[] freqs = new int[NUM_ASCII_CHAR];
  91. File f = new File(fileName);
  92. FileReader r = new FileReader(f);
  93. while( r.ready() ){
  94. int ch = r.read();
  95. // if( 0 <= ch && ch <= NUM_ASCII_CHAR)
  96. // freqs[ch]++;
  97. // else
  98. // freqs[INDEX_NON_ASCII]++;
  99. if(0 <= ch && ch < freqs.length)
  100. freqs[ch]++;
  101. else
  102. System.out.println((char) ch);
  103. }
  104. r.close();
  105. return freqs;
  106. }
  107. }