Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Strings. 5 Comments on Java program to print all unique words of a string

In this article we will learn to implement a java program to print all unique words of a string. A java program is provided below which reads a line of text from the user and prints out the unique words in ascending order.

 

We use TreeMap class from collections framework to store the frequency of each word in the given line of text and print out only the words with frequency 1, which will give us the unique words.

 

Program is as follows:

import java.util.*;

public class Driver
{
	public static void main(String[] args)
	{
		System.out.println("Enter a line of text: ");
		Scanner input = new Scanner(System.in);
		String s = input.nextLine();
		input.close();
		TreeMap<String, Integer> list = new TreeMap<String, Integer>();
		for(String str : s.split(" "))
		{
			if(list.containsKey(str))
			{
				list.put(str, list.get(str)+1);
			}
			else
			{
				list.put(str, 1);
			}
		}
		for(Map.Entry e: list.entrySet())
		{
			if((int)e.getValue() == 1)
				System.out.println(e.getKey());
		}
	}
}

 

Input and output for the above program is as follows:

Input: 
Enter a line of text: 
i am a good am i boy

Output:
a
boy
good

 

This program can also be tweaked to print the frequency of each word in a given line of text and also this can be extended to read a file line-by-line and find the unique words, frequency of each word, or the word that repeats the most and so on.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

5 Comments

You can follow any responses to this entry through the RSS 2.0 feed.

Very informative article….

Without using collection framework can we do this coding???

    Yes. We can do it.
    You can use StringTokenizer class and some datastructure and implement it.

nice article for beginners.thank you.

Good morning sir -Suryateja Pericherla . I read your blog very suitable sllybus and with eg video .

Leave a Reply

Your email address will not be published. Required fields are marked *