|
-
Jan 9th, 2003, 03:24 AM
#1
Thread Starter
Addicted Member
searching a text file for a specific word
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();}
}}
-
Jan 9th, 2003, 11:58 AM
#2
for (int i = 0;i < CurrentString.length();i++)
{if(CurrentString.charAt(i) == ' ')
{numberofCharacters++;}}
This is surely not what you want.
This is too much code for me to look all the way through. Check the reference of String for functions you might find helpful.
And be careful: 's' is a character, "s" is a string. 'the' is nothing. The compiler might eat it and make a UNICODE character from it, but it's not the string "the" you want.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 9th, 2003, 01:12 PM
#3
Thread Starter
Addicted Member
thanks for your reply. I think that I understand what you are saying - I'll go away and play with my code a bit more
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|