Rajbir Bhullar
FileWords
1 Answer(s)      5 months and 17 days 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.

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 Pages:
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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.