I am writing a program that will read a text file and calculate number of words/characters etc. That bit works fine, but I am struggling on how to find a specific word(in this case 'The' - I also need it to be able to ignore case)
each time I search for ' the ' (leading and follwing space) I get an error 'unclosed character literal'
Please suggest where I am going wrong (I'm a beginner, so please go easy on the techical talk!)
Code:(I haven't included keyboard3.java or text file) import javax.swing.JOptionPane; public class MSShowPane2{ char choice; private String s; String CurrentString = s; int countspaces = 0; int i = 0; int countfullstop = 0; int countcommas = 0; int AverageWordLength = 0; int numberofCharacters = 0; int NumberofCharacters = 0; int numberofCharacters2 = 0; int NumberofCharacters2 = 0; int CountCommas = 0; int CountWords = 0; int countwords = 0; int num=0; int countthe = 0; //int CheckThe = 0; String testString = CurrentString; public void LoadText1() { s = Keyboard3.readTextFile("Shirley.txt"); System.out.println("Shirley By Bronte Sisters ... Loaded "); } //public void LoadText2() { //s = Keyboard3.readTextFile("Villette.txt"); //System.out.println("Villette By Bronte Sisters ... Loaded "); //} public void AverageWordLength() { String CurrentString = s; for (int i = 0;i < CurrentString.length();i++) {if(CurrentString.charAt(i) == ' ') {countspaces++;}} int AverageWordLength = s.length()/ countspaces; System.out.println("Average Word Length: " + AverageWordLength + " Characters"); } public void AverageSentanceLength () { String CurrentString = s; for (int i = 0;i < CurrentString.length();i++) {if(CurrentString.charAt(i) == '.') {countfullstop++;}} int AverageSentanceLength = s.length()/ countfullstop; System.out.println("Average Sentance Length: " + AverageSentanceLength + " Characters"); } public void NumberofCharacters () { String CurrentString = s; for (int i = 0;i < CurrentString.length();i++) {if(CurrentString.charAt(i) == ' ') {numberofCharacters++;}} for (int i = 0;i < CurrentString.length();i++) {if(CurrentString.charAt(i) == ' ') {countspaces++;}} NumberofCharacters = s.length()- countspaces; NumberofCharacters2 = s.length(); System.out.println("Number of characters: " + NumberofCharacters + " Characters (No Spaces)"); System.out.println("Number of characters: " + NumberofCharacters2 + " Characters (With Spaces)"); } public void CountCommas() { String CurrentString = s; for (int i =0;i < CurrentString.length() ;i++ ) {if (CurrentString.charAt(i) == ',') {countcommas++;}} int CountCommas = countcommas; System.out.println("Number of Commas: " + countcommas); } public void CountWords() { //Assuming each word is followed by a space. String CurrentString = s; for (int i =0;i < CurrentString.length() ;i++ ) {if (CurrentString.charAt(i) == ' ') {countspaces++;}} int CountWords = countspaces +1; System.out.println("Number of Spaces: " + countspaces); System.out.println("Number of Words: " + CountWords); } public void CheckThe() { String CurrentString = s; for (int i =0;i < CurrentString.length() ;i++ ) {if (CurrentString.charAt(i) == 'the') {countthe++;}} int CheckThe = countthe; System.out.println("Number of occurances of the word 'The': " + countthe); } public void mainLoop() { do { JOptionPane.showMessageDialog( null,"Menu:\n\n A: Load Text 1\n B: Load Text 2\n C: Average Word Length\n D: Average Sentance Length\n E: Number of Characters\n F: Number of Commas\n G: Number of The's\n H: Total Number of Words\n I: Number of A's\n X: EXIT","Welcome To Sarah's Metric Analysis", JOptionPane.PLAIN_MESSAGE); choice = Keyboard3.readChar(); switch(choice) { case 'A': {LoadText1();} break; //case 'B': {LoadText2();} //break; case 'C': {AverageWordLength();} break; case 'D': {AverageSentanceLength ();} break; case 'E': {NumberofCharacters();} break; case 'F': {CountCommas();} break; case 'G': {CheckThe();} break; case 'H': {CountWords();} break; case 'X': System.exit(0); }} while (choice!='X'); } public static void main(String[]args){ {MSShowPane2 am = new MSShowPane2(); am.mainLoop();} }}




Reply With Quote