Click here to Skip to main content
16,007,760 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am asked to write a program to read from a text file and display nonduplicate words in ascending order. The text file is passed as a command-line argument. Basically, I've managed to write a program to read a predefined String and display all nonduplicates. All is left to do is to read from a file. This is something I am not able to comprehend. Thanks a lot. Here is my code:

import java.util.*;

public class NonduplicateWords {
	public static void main(String[] args) {

		String text = "Testing. I have a testing text. Have a great day";

		TreeSet<String> set = new TreeSet<String>();
		String[] words = text.split("[ \n\t\r.,;:!?(){}]");
		for (int i = 0; i < words.length; i++) {
			String value = words[i].toLowerCase();
			if (words[i].length() > 0)
				set.add(value);
		}
		for (Object element: set)
			System.out.println(element.toString() + " ");
	}
}
Posted
Updated 25-Sep-10 13:33pm
v2

 
Share this answer
 
Comments
Sandeep Mewara 26-Sep-10 5:48am    
Comment from OP:
I googled a lot. I need someone to explain it all, and how to read in a file and, say, split the words and store in an ArrayList, or some other way. I just don't know the file input part of this. If someone could give me a small example on how to use it in my situation, I'd really appreciate it.
Try the Java Tutorials[^]; there are samples of just about everything you need.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900