Drop Down

Monday, February 4, 2019

Find No. of Words: No. of characters: List to duplicate Words from text File

package test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/*
No. of Words:
No. of characters:
List to duplicate Words:
D:\work\Java Work Space\files\amit.txt
*/
public class Count_Duplicate_Words_From_File
{
public static void main(String[] args) throws IOException, FileNotFoundException
{
  BufferedReader br = new BufferedReader(new FileReader("D:\\work\\Java Work Space\\files\\amit.txt"));
  StringBuffer sb = new StringBuffer();
  String s = br.readLine();
  while(s!= null)
  {
  //sb = sb.append(s); //----->White ko hatna hetu
  sb = sb.append(s.trim());
  sb = sb.append(" ");
  s = br.readLine();
  }
  br.close();
  String[] words = sb.toString().split(" ");
  wordCount(words);
  charCount(words);
  DupWords(words);
 
}

private static void DupWords(String[] words)
{
  Map <String,Integer> map = new HashMap<>();
  for(String s : words)
  {
  if(!map.containsKey(s))
  {
  map.put(s, 1);
  }
  else
  {
  map.put(s, map.get(s)+1);
  }
  }
  System.out.println("List to duplicate Words:");
  for(Map.Entry<String,Integer> entry : map.entrySet())
  {
  if(entry.getValue()>1)
  System.out.println(entry.getKey()+" : "+entry.getValue());
  }

}

private static void charCount(String[] words)
{
char[] ch;
int count = -1;
  for (String s : words)
  {
for(int i = 0;i<s.length();i++)
{
count++;
}
count++;
  }
  System.out.println("No. of characters: "+count);

}

private static void wordCount(String[] words)
{
  System.out.println(Arrays.toString(words));
  System.out.println("No of Words: "+words.length);

}
}

No comments:

Post a Comment

Java 8 Notes Pics