Showing posts with label count. Show all posts
Showing posts with label count. Show all posts

Java program that counts words in a file

Question:
Write a java program that counts words in a file?

Answer:
Here is an example that counts the words in a file called TheCatInTheHat.txt.

Code:
import java.io.*;
import java.util.*;
 
public class WordCount {
  public static void main(String[] args) throws FileNotFoundException {
    Map<String, Integer> map = new HashMap<String, Integer>();
 
    Scanner in = new Scanner(new File("TheCatInTheHat.txt"));
    while (in.hasNext()) {
      String word = in.next();
      word = word.replaceAll("[^a-zA-Z0-9]", "");
      word = word.toLowerCase();
 
      if(map.containsKey(word)) {
        Integer count = (Integer)map.get(word);
        map.put(word, new Integer(count.intValue() + 1));
      } else {
        map.put(word, new Integer(1));
      }
    }
 
    ArrayList arraylist = new ArrayList(map.keySet());
    Collections.sort(arraylist);
 
    for (int i = 0; i < arraylist.size(); i++) {
      String key = (String)arraylist.get(i);
      Integer count = (Integer)map.get(key);
      System.out.println(key + " : " + count);
    }
 
  }
}

Output:
$ java WordCount
a : 33
about : 3
after : 1
all : 15
always : 1
and : 69
another : 2
any : 1
are : 5
as : 9
asked : 1
at : 14
away : 6
back : 2
bad : 2
ball : 5
be : 7
bed : 1
bent : 1
bet : 2
big : 5
bit : 3
bite : 1
.......

How to write a program that counts the number of words and lines in a file?

Question:
How to write a program that counts the number of words and lines in a file?

Code:
import java.util.*;
import java.io.*;
 
public class CountWords {
  public static void main(String[] args) throws FileNotFoundException {
    File file = new File("example.txt");
    Scanner in = new Scanner(new FileInputStream(file));
    countWords(in);
  }
 
  // Counts total lines and words in the input scanner.
  public static void countWords(Scanner input) {
 
    int lineCount=0;
    int wordCount=0;
 
    while(input.hasNextLine()) {
      String line = input.nextLine();
      lineCount++;
 
      String str[] = line.split((" "));
      for (int i=0;i<str.length;i++) {
        if (str[i].length()>0) {
          wordCount ++;
        }
      }
    }
 
    System.out.println("Lines: " + lineCount);
    System.out.println("Words: " + wordCount);
  }
}
 

Output:
$ cat example.txt 
this is line one 
this is line two
this is line three

$ java CountWords
Lines: 3
Words: 12

How to count words in a sentence?

Question:
How to count words in a sentence?

Code:
import java.util.*;
 
public class CountWords {
   public static void main(String[] args) {
 
      System.out.print("Enter a string: ");
 
      Scanner sc = new Scanner(System.in);
      String str = sc.nextLine();
 
      String[] words = str.split(" ");
 
      System.out.println("String: " + str);
      System.out.println("Total Words: " + words.length);
 
      sc.close();
   }
}

Output:
$ java CountWords 
Enter a string: To be or not to be
String: To be or not to be
Total Words: 6