Code to Count no of Words in a File

Hi pals,
Yesterday, I had a telephonic interview and the interviewer asked to create this program. Hope it might be helpful for you. He left the decision of choosing the programming to chose on me. The interviewer was a Window User and I prefer GNU/Linux. Therefore, I decided to code in Java. Because the program compiled in java is machine independent. It only requires Java Runtime Environment to run on the target platform.
Now let’s look at the code.

In this we will create to classes one we name Word (Word.java) which contains the structure of the words. and the other is Counter (Counter.java) which will contain all the functions for text processing of the file.

Word.java

public class Word{
	int 	n; 	// occurence of the word
	String 	word; 	// text of the word itself

	Word(String word){
		this.word = word;
		n = 1;
	}
}

Counter.java

public class Counter{
	static Word words[] = new Word[1000];
	static int  total   = 0;

	public static void main(String arg[]){
		String 	target = ""; 	// will hold the text of file
		String 	token = ""; 	// will be used to read each word
		char 	ch;		// will be used to read each character

		/* placing text of file into target string */
		target = readFile(arg[0]);

		/* reading each word from target string */
		for(int i=0; i<target.length();i++){
			ch = target.charAt(i);
			/* weeding out the separating characters for the words */
			if(ch == ' ' || ch == '.' || ch == ',' || ch == '\n' || ch == '\t' || ch =='\0'){
				if(token.length() == 0)continue;
				
				if(chekWordExists(token)){
					token = "";
				}else{

					words[total++] = new Word(token);
					token = "";
				}
			}else{
				token += ch;
			}
		}

		System.out.println("*************Actual Order**************");
		for(int i=0; i<total; i++){
			System.out.println(words[i].word+ ":" + words[i].n);
		}
		sortWords();
		System.out.println("*************Counting Order**************");
		for(int i=0; i<total; i++){
			System.out.println(words[i].word+ ":" + words[i].n);
		}
		
		sortAlphaWords();
		System.out.println("*************Alphabatic Order**************");
		for(int i=0; i<total; i++){
			System.out.println(words[i].word+ ":" + words[i].n);
		}
		

	}

	/* function to chek the word if it has already been read if so then increment it's occurence */
	public static boolean chekWordExists(String token){
		for(int i=0; i<total; i++){
			if(words[i].word.equals(token)){
				words[i].n++;
				return true;
			}
		}
		return false;
	}
	
	/* function to sort the words according to their occurences */
	public static void sortWords(){
		for(int i=0; i<total; i++){
			for(int j=1; j<total; j++){
				if(words[j-1].n > words[j].n){
					Word temp = words[j-1];
					words[j-1] = words[j];
					words[j] = temp;				
				}
			}
		}

	}

	/* function to sort the array of words alphabatecaly */
	public static void sortAlphaWords(){
		for(int i=0; i<total; i++){
			for(int j=1; j<total; j++){
				if(words[j-1].word.compareTo(words[j].word)>0){
					Word temp = words[j-1];
					words[j-1] = words[j];
					words[j] = temp;				
				}
			}
		}

	}

	/* function to read a file and return the whole text as a string */
	public static String readFile(String arg){
		File file = new File(arg);
		int ch;
		StringBuffer content = new StringBuffer("");
		FileInputStream fin = null;
		try{
			fin = new FileInputStream(file);
			while((ch = fin.read()) != -1){
				content.append((char)ch);
			}
			fin.close();
		}catch(IOException e){
			System.out.println("An exception occured.");
		}
		return content.toString();
	}

}

Now Compile both the Source Files using javac Compiler

$ javac Word.java Counter.java

The above command will result in two class files Word.class and Counter.class , Now run the Counter class by invokin java virtual machine and passing the Counter Class name along with the filename of text which you want to process.

$ java Counter TEXT_FILE_NAME

The above command will display the result on the console. You can also redirect the output of the program to another file which you can read later to review the result. as follows

$ java Counter TEXT_FILE_NAME > RESULT_FILE

If in doubt, feel free to post your queries.

Twit this with your twitter acount :

One thought on “Code to Count no of Words in a File

  1. Pingback: max

Leave a Reply