Monday, June 19, 2017

Write a program to get distinct word list from the given file.


Description:
Write a program to find all distinct words from the given file. Remove special chars like ".,;:" etc. Ignore case sensitivity.

Code:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.govindsblog.algos;
 
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
 
public class MyDistinctFileWords {
 
    public List<String> getDistinctWordList(String fileName){
 
        FileInputStream fis = null;
        DataInputStream dis = null;
        BufferedReader br = null;
        List<String> wordList = new ArrayList<String>();
        try {
            fis = new FileInputStream(fileName);
            dis = new DataInputStream(fis);
            br = new BufferedReader(new InputStreamReader(dis));
            String line = null;
            while((line = br.readLine()) != null){
                StringTokenizer st = new StringTokenizer(line, " ,.;:\"");
                while(st.hasMoreTokens()){
                    String tmp = st.nextToken().toLowerCase();
                    if(!wordList.contains(tmp)){
                        wordList.add(tmp);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try{if(br != null) br.close();}catch(Exception ex){}
        }
        return wordList;
    }
     
    public static void main(String a[]){
         
        MyDistinctFileWords distFw = new MyDistinctFileWords();
        List<String> wordList = distFw.getDistinctWordList("C:/sample.txt");
        for(String str:wordList){
            System.out.println(str);
        }
    }
}

Output:
the
while
statement
verifies
condition
before
entering
into
loop
to
see
whether
next
iteration
should
occur
or
not
do-while
executes
first
without
checking
it
after
finishing
each
will
always
execute
body
of
a
at
least
once

Sunday, June 18, 2017

Write a program to get a line with max word count from the given file.


Description:
Below example shows how to find out the line with maximum number of word count in the given file. In case if it has multiple lines with max number of words, then it has to list all those lines.

Code:
package com.govindsblog.algos;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class MaxWordCountInLine {

	private int currentMaxCount = 0;
	private List<String> lines = new ArrayList<String>();
	
	public void readMaxLineCount(String fileName){

		FileInputStream fis = null;
		DataInputStream dis = null;
		BufferedReader br = null;
		
		try {
			fis = new FileInputStream(fileName);
			dis = new DataInputStream(fis);
			br = new BufferedReader(new InputStreamReader(dis));
			String line = null;
			while((line = br.readLine()) != null){
				
				int count = (line.split("\\s+")).length;
				if(count > currentMaxCount){
					lines.clear();
					lines.add(line);
					currentMaxCount = count;
				} else if(count == currentMaxCount){
					lines.add(line);
				} 
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try{
				if(br != null) br.close();
			}catch(Exception ex){}
		}
	}

	public int getCurrentMaxCount() {
		return currentMaxCount;
	}

	public void setCurrentMaxCount(int currentMaxCount) {
		this.currentMaxCount = currentMaxCount;
	}

	public List<String> getLines() {
		return lines;
	}

	public void setLines(List<String> lines) {
		this.lines = lines;
	}
	
	public static void main(String a[]){
		
		MaxWordCountInLine mdc = new MaxWordCountInLine();
		mdc.readMaxLineCount("/Users/ngootooru/MyTestFile.txt");
		System.out.println("Max number of words in a line is: "+mdc.getCurrentMaxCount());
		System.out.println("Line with max word count:");
		List<String> lines = mdc.getLines();
		for(String l:lines){
			System.out.println(l);
		}
	}
}

MyTestFile.txt:
true, false, and null might seem like keywords, but they are actually literals.
You cannot use them as identifiers in your programs. The servlet context
is an interface which helps to communicate with other servlets. It contains
information about the Web application and container. It is kind of
application environment. Using the context, a servlet can obtain URL
references to resources, and store attributes that other servlets in the
context can use.

Output:
Max number of words in a line is: 13
Line with max word count:
true, false, and null might seem like keywords, but they are actually literals.