Write a program called FileWords that reads words from a text file and displays all the non-duplicate words in ascending order.
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()); } }
Ads