• No results found

The following program is aiming to extract from a simple text file an analysis of the content such as:

N/A
N/A
Protected

Academic year: 2021

Share "The following program is aiming to extract from a simple text file an analysis of the content such as:"

Copied!
7
0
0

Loading.... (view fulltext now)

Full text

(1)

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.

(2)

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

(3)

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 }

(4)

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

(5)

{

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 }

(6)

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.

(7)

References

Related documents

Compared to greater Pakistan, the FATA is suffering from lower levels of investment in health capital, education and economic development?. All of these factors contribute to

In this PhD thesis new organic NIR materials (both π-conjugated polymers and small molecules) based on α,β-unsubstituted meso-positioning thienyl BODIPY have been

For the poorest farmers in eastern India, then, the benefits of groundwater irrigation have come through three routes: in large part, through purchased pump irrigation and, in a

Other insurance Except for Section 2 - Personal Accident, if the Insured Person is able to claim under any other policies (including statutory insurance and/or

We have loved them during life, let us not abandon them until we have conducted them, by our prayers, into the House of the Lord.. Jesus, have mercy on the

Commercial aircraft programs inventory included the following amounts related to the 747 program: $448 of deferred production costs at December 31, 2011, net of previously

c+c%+c'ccc#c c Œou shouldn¶t go to India without visiting the ajMahal.c Oo deberías ir a la India sin visitar el TajGahal.c I¶minterested in studyingpsychology.c!c@stoy interesado

(a) the entrant does not want the power exercised in relation to the entrant’s person or belongings and is prepared to leave the state building immediately and take the belongings;