Skip to content

Instantly share code, notes, and snippets.

@kevinbarbour
Created September 4, 2012 17:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinbarbour/3623963 to your computer and use it in GitHub Desktop.
Save kevinbarbour/3623963 to your computer and use it in GitHub Desktop.
CMSC 256 Project 1
/* Student: Kevin Barbour
* Course: Computer Science 256
* Professor: Dr. Parker
* Assignment 1
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.lang.reflect.Array;
import java.lang.Character;
/* Class PA1KDB
*
* Analyzes a text file and prints the number of times each unique letter
* occured in the file.
*/
public class PA1KDB
{
/* method main
*
* Takes the name of a text file as an argument, cycles through each character in the
* file counting the occurances of each unique letter, then prints a formatted
* table showing how many times each occured.
*/
public static void main(String[] args)
{
// Make sure an argument was given
if(args.length == 0){
System.out.println("No argument given. Run as 'java PA1KDB <filename>'");
System.exit(0);
}
// Get text from file as string
String file_name = args[0];
String file_string = readFile(file_name);
// Make array of characters from string
char file_chars[] = file_string.toCharArray();
// Make array to keep count of each letter (indexed at <ascii code for letter> - 'A')
int char_counts[] = new int[58];
// Loop through characters adding one to each count when a letter is found
for(int i = 0; i < Array.getLength(file_chars); i++) {
// Check if the character is actually a letter
if(Character.isLetter(file_chars[i])) {
// Add to count for letter in array
char_counts[file_chars[i] -'A']++;
}
}
System.out.println("CMSC 256 Project #1 - K. Barbour\n");
System.out.println("Input file:" + file_name);
System.out.println("Contents:\n" + file_string);
printResults(char_counts);
}// main()
/* method readFile
*
* Takes the name of a file as a string input. Creates a string from the text
* in the file, and returns the string.
*/
static String readFile(String file_name) throws FileNotFoundException, IOException
{
File input_file = new File(file_name);
Scanner file_reader = new Scanner(input_file);
// Make string from file
String file_string = file_reader.useDelimiter("\\Z").next();
return file_string;
}// readFile()
/* method printResults
*
* Takes an array of integers as input. Loops through each index of the array
* printing the occurances of the character if any.
*/
static void printResults(int[] char_counts)
{
System.out.println("\nLetter Count\n------ -----");
// Print lowercase results
for(int i = 32; i < 58; i++) {
//if(low_char_counts[i] > 0) {
if((char_counts[i]) > 0) {
System.out.printf(" %c\t%d\n", (char) (i + 'A'), char_counts[i]);
}
}
// Print uppercase results
for(int i = 0; i < 26; i++)
if((char_counts[i]) > 0) {
System.out.printf(" %c\t%d\n", (char) (i + 'A'), char_counts[i]);
}
}// printResults()
}// class PA1KDB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment