Print all unique words from a string in Java

Hi guys, the following tutorial will demonstrate how to develop a Java program that prints all the unique words in a string entered by the user. In this program, I have used Scanner class to take input from user.

First, I will show you the code then I will explain it to you. So, let’s start with the code first.

import java.util.Scanner;
public class first
{
  public static void main(String ar[]) {
    String str;
    Scanner sc=new Scanner(System.in);
    str=sc.nextLine();
    String words[]=str.split(" ");
                
    int count=0;
    for(int i=0;i<words.length;i++)
    {  if(words[i].equals("") || words[i].equals(" "))
          {                      
            continue; 
          }
       else {                  
              for(int j=i+1;j<words.length;j++)
              {
                if(words[i].equals(words[j]))
                 { 
                   count=1;
                   words[j]=" ";
                   break;
                 }
              }
              if(count==0)
              {
               System.out.print(words[i]+" ");
              }
              count=0;     
            }
    }
  }
}

 

First, user will enter the string using Scanner class

To use Scanner class, first we have to import it:

import java.util.Scanner;

Then I have declared a class named first, which contains a static void main function in which contains main part of our program.

In line 6, in the above code, I have declared a new object sc of Scanner class to use various built-in methods provided by Scanner class. Here, I have passed a predefined object System.in which is used to take standard input(basically, to take input from a user). Then, in line 7, I have used nextLine() method of Scanner to take input from the user as a string.

At line 8, I have used String method split() to extract words from the string entered by the user and stored them in the array of String words.

import java.util.Scanner;
public class first
{
  public static void main(String ar[]) {
    String str;
    Scanner sc=new Scanner(System.in);
    str=sc.nextLine();
    String words[]=str.split(" ");

Now, we will print the unique words from the string

In this part, I will be using two for loops to check if any word is repeated or not in the string. If a word is not repeated then I will print it. Here, we will also be using count variable which tells us that a particular word is repeated or not.

For outer loop when i=0, inner loop will traverse from j=i+1 to j<words.length

when i=1, inner loop will again traverse from j=i+1 to j<words.length and so on until i<words.length.

During first iteration i=0, outer loop will select 1st word, we first check whether the word selected is a space or not, if it is then we will skip that particular iteration otherwise, we will proceeds.

Then inner loop will check by comparing every word contained in the words String array with the word selected by outer loop with the help of equals() String method and if it matches then set count=1 and words[j]=” ” so that the repeated word is removed from the words array and then come out of the inner array.

int count=0;
for(int i=0;i<words.length;i++)
{
  if(words[i].equals("") || words[i].equals(" "))
  {                      
   continue;
  }
  else {                  
       for(int j=i+1;j<words.length;j++)
       {
        if(words[i].equals(words[j]))
        { 
          count=1;
          words[j]=" ";
          break;
        }
       }
       if(count==0)
       {
         System.out.print(words[i]+" ");
       }
       count=0;     
     }
}

 

Then, after every complete iteration of inner loop we will check whether count is set=1 or not, if it is not we will print the ith word in words array otherwise, we set count=0 again for next iteration we will keep repeating this until all word in words array got checked.

Input:

this is not me and this is not you how are you

Output:

me and how are

Hoping, that this blog helps you.

Also, read: How to extract Date from a String in Java

Leave a Reply