Text Analyser Aim The following program is aiming to extract from a simple text file an analysis of the content such as: Number of printable characters Number of white spaces Number of vowels Number of consonants Frequency of each vowel PseudoCode This is the original Pseudocode I had noted down for my original code ideas. Start End IF no file or text is entered in programme, request file from users ELSE continue SORT the text in the file COUNT the number of characters and variations OUTPUT the count When I started trying to put code to paper as it were, I came across a number of stumbling blocks and so I went and did some research in various sources to try and over come these issues. One of the main issues I had was in trying to get the programme to read in the text from a simple text file (my original aim was to use something in the same feel as page 464 of Java in two semesters) and so I resorted to requesting an input from the keyboard. Upon completion of my research, I found that the Pseudocode used in my original ideas was not very similar. Cormac O'Connell Page 1 of 7 5/14/2007
This is the revised Pseudocode: Start END INPUT string of characters COUNT text length COUNT vowels COUNT consonants (length vowels) COUNT white space (space bar & tabs depressions) OUTPUT counts Cormac O'Connell Page 2 of 7 5/14/2007
The Code The following is the print out of my java code. I have highlighted using comments the bits that were my own work and the bits that were the work of someone else (The code was found during my research into the problems I was having). // my bit import java.util.*; // required for Scanner public class textanalyser // the main class that will do all the work static char[] vowel='a', 'e', 'i', 'u', 'o'; // set the definition of a vowel. This could easily be changed to consonants by just replacing the characters present public static void main(string[] args) System.out.println("Enter a word, or phrase (Maximum 300 words):"); // request the user input the text to be analysed Scanner sc = new Scanner(System.in); // read the input from Scanner into sc String text = sc.nextline(); // read the text input by the user into the Scanner sc System.out.println("You entered the following text: "+text); // display the number of characters typed System.out.println("Number of characters: "+text.length()); // display the number of printable characters System.out.println("Number of vowels: "+countvowels(text)); // display the number of vowels System.out.println("Number of constanants: "+(text.length()-countvowels(text))); // display the number of consonants System.out.println("Number of spaces: "+countspaces(text)); // display the number of white spaces // end of my bit // bit from http://java.sun.com static int countvowels(string s) // method to count the number of vowels input char[] array = s.tochararray(); int counter=0; for (int i = 0; i < array.length; i++) if(isvowel(array[i])) counter++; return counter; static boolean isvowel(char c) for (int i = 0; i < vowel.length; i++) if(c==vowel[i]) return true; return false; spaces static int countspaces(string s) // method to count the number of white int count=0; char[]a=s.tochararray(); for (int i = 0; i < a.length; i++) if(32==(int)a[i]) //32 = (int)' ' count++; return count; // end of the bit from http://java.sun.com Cormac O'Connell Page 3 of 7 5/14/2007
Testing Unfortunately, I was unable to create a test rig to test this programme. However I tested it by hand and it seems to work okay. One thing I have realised is that although my program seems to work okay, it detects symbols as consonants. This will require a little bit of work to rectify. The following is the code I am trying to use to rectify the issue, however it doesn t work in it s current state. // my bit import java.util.*; // required for Scanner public class textanalyser1 // the main class that will do all the work static char[] vowel='a', 'e', 'i', 'o', 'u'; // set the definition of a vowel. This could easily be changed to consonants by just replacing the characters present static char[] consonant='b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'; // set the definition of a consonant public static void main(string[] args) System.out.println("Enter a word, or phrase (Maximum 300 words):"); // request the user input the text to be analysed Scanner sc = new Scanner(System.in); // read the input from Scanner into sc String text = sc.nextline(); // read the text input by the user into the Scanner sc System.out.println("You entered the following text: "+text); // display the number of characters typed System.out.println("Number of characters: "+text.length()); // display the number of printable characters System.out.println("Number of vowels: "+countvowels(text)); // display the number of vowels System.out.println("Number of constanants: "+countconsonants(text)); // display the number of consonants System.out.println("Number of symbols: "+(text.length()- countvowels(text)-countconsonants(text)); // display the number of consonants System.out.println("Number of spaces: "+countspaces(text)); // display the number of white spaces // end of my bit static int countconsonants(string s) // method to count the number of consonants input char[] array = s.tochararray(); int counter=0; for (int i = 0; i < array.length; i++) if(isconsonant(array[i])) counter++; return counter; static boolean isconsonant(char c) for (int i = 0; i < consonant.length; i++) if(c==consonant[i]) return true; return false; // bit from http://java.sun.com static int countvowels(string s) // method to count the number of vowels input Cormac O'Connell Page 4 of 7 5/14/2007
char[] array = s.tochararray(); int counter=0; for (int i = 0; i < array.length; i++) if(isvowel(array[i])) counter++; return counter; static boolean isvowel(char c) for (int i = 0; i < vowel.length; i++) if(c==vowel[i]) return true; return false; static int countspaces(string s) // method to count the number of white spaces int count=0; char[]a=s.tochararray(); for (int i = 0; i < a.length; i++) if(32==(int)a[i]) //32 = (int)' ' count++; return count; // end of the bit from http://java.sun.com Cormac O'Connell Page 5 of 7 5/14/2007
References During the course of my research and code I made general reference to the following: Java in two semesters, 2 nd Edition, Charatan & Kans, McGraw Hill publishing http://java.sun.com and the forums found there in. Direct reference was made to http://java.sun.com forums in my code and these sections have been highlighted with comments. Cormac O'Connell Page 6 of 7 5/14/2007
Cormac O'Connell Page 7 of 7 5/14/2007