GTU OOP Program - 25

25). Write a program that reads words from a text file and displays all the nonduplicate words in descending order.The text file is passed as a command-line argument.

  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class Program_25 {
  5. public static void main(String[] args) throws Exception {
  6. File fin = new File(args[0]);
  7. BufferedReader br = new BufferedReader(new FileReader(fin));
  8. StringBuffer buffer = new StringBuffer();
  9. String str;
  10.  
  11. while ((str = br.readLine()) != null) { // reading the text file
  12. buffer.append(str); // storing text in StringBuffer
  13. buffer.append(" "); // Separating words by spaces
  14. }
  15. ArrayList list = new ArrayList(); // Declaring ArrayList
  16. StringTokenizer st = new StringTokenizer(buffer.toString().toLowerCase());
  17. while (st.hasMoreTokens()) { // creating a list of words
  18. String s = st.nextToken();
  19. list.add(s);
  20. }
  21. HashSet set = new HashSet(list); // it is created to avoid duplicate
  22. List arrayList = new ArrayList(set); // creating list of words
  23. Comparator c = Collections.reverseOrder();
  24. Collections.sort(arrayList, c);
  25. for (Object obj : arrayList) {
  26. System.out.println(obj.toString()); // displaying content
  27. }
  28. }
  29. }
Input.txt file
cse gtu, gtu study material for computer engineering students gujarat technological university oop lab manual solution
Output
cse gtu, gtu study material for computer engineering students gujarat technological university oop lab manual solution

gtu oop lab manual,

oop GTU Practical,

oop Programs,

Object oriented Programming,

Gtu study material 3140705,

OOP 3140705,

oop practical list,

Java Programming,

gtu oop practical,

oop lab solution gtu

Comments

YouTube

Popular posts from this blog

GTU OS Program - 11 Filters

GTU OOP Program - 17 tic-tac-toe

GTU OS Program - 13 Date Validator