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
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
End
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.
This is the revised Pseudocode:
Start
INPUT string of characters
COUNT text length
COUNT
vowels
COUNT consonants (length – vowels)
COUNT white space (space bar & tabs depressions)
OUTPUT
counts
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; }
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 }
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
{
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 }
References
During the course of my research and code I made general reference to the following:
Java in two semesters, 2
ndEdition, 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.