Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


Rajbir Bhullar
FileWords
1 Answer(s)      a year and 9 months ago
Posted in : Java Beginners


Write a program called FileWords that reads words from a text file and displays all the non-duplicate words in ascending order.


Advertisement
View Answers

December 6, 2012 at 11:59 AM


Here is a java example that reads words from a text file and displays all the non-duplicate words in ascending order.

import java.io.*;
import java.util.*;

class FileWords 
{
    public static void main(String[] args) throws Exception
    {
        Scanner input=new Scanner(System.in);
        System.out.println("Enter File name with path: ");
        String filename=input.next();
        File f=new File(filename);
        BufferedReader br=new BufferedReader(new FileReader(f));
        StringBuffer buffer=new StringBuffer();
        String str;
        while((str=br.readLine())!=null){
        buffer.append(str);
        buffer.append(" ");
        }
        ArrayList<String> list=new ArrayList<String>();
        StringTokenizer st = new StringTokenizer(buffer.toString().toLowerCase());
                while(st.hasMoreTokens()) {
                        String s = st.nextToken();
                        list.add(s);
                }
        HashSet<String> set = new HashSet<String>(list);
        List<String> arrayList = new ArrayList<String>(set);
        Collections.sort(arrayList);
        for (Object ob : arrayList)
        System.out.println(ob.toString());
    }
}


Related Tutorials/Questions & Answers:
FileWords
FileWords  Write a program called FileWords that reads words from a text file and displays all the non-duplicate words in ascending order
Display non-duplicate words from file
java.util.*; class FileWords { public static void main(String[] args
Advertisements
 

 

 

DMCA.com