Tutorials Hut

Java Program To Validate Statement In Dictionary

 

In this article we will write a java program to validate statements in a dictionary.

Problem statement: If we have a string statement, containing different words and another string called a dictionary containing valid words; and if all words of statement are present in the dictionary then it is valid else it is invalid.

Example 1:

String statment = "I fox am";
String disctonary = "fox I am";

Statement is valid since I, fox and am all words are in the dictionary.

Example 2:

String statment = "I am a boy";
String disctonary = "fox I am";

Statement is invalid because not all words of the statement are present in the dictionary.

Java Program To Validate Statement In Dictionary

package problems;
import java.util.*;

public class CheckValidStatmentInDict {
public static void main(String[] args)
    String statment = "I fox am";
    String disctonary = "fox I am";
    String[]  statArray = statment.split(" ");
     String[]  dictArray = disctonary.split(" ");

    List<String> statList = Arrays.asList(statArray);
     List<String> dictList = Arrays.asList(dictArray);

    Boolean result = true;
    for(String word: statList) {
        if(!dictList.contains(word)) {
            result = false;
            break;
        }
    }
   System.out.println(result);
}
}

Output:

True

 

 


















  • Leave a Reply

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