string - How to compare character input by user to dictionary file in Java? -


i need read user input , compare dictionary.txt. user may input number of characters , program must return words in english language can made these characters. letters can used in order , may used once. example:

user input: "odg"

output: "dog" , "god" ... , others

after quite substantial amount of research, have come following partial solution:

  1. read user input
  2. convert array of characters
  3. loop through document depending on array length
  4. using indexof compare each character in array each line, printing word/s not return -1

how compare set of characters inputted user found in text file (dictionary) ? characters not have in order match .(as seen in example used above)

bear me here, know must 1 of inefficient ways such task! further ideas on how implement original idea appreciated, while open new , more efficient methods perform operation.

below have come far:

  public static void main(string[] args) throws filenotfoundexception {     bufferedreader reader1 = new bufferedreader(new filereader(filename));     scanner sc = new scanner(system.in);     string line;     arraylist<string> match = new arraylist<>();      system.out.println("enter characters see english words match: ");     string userinput = sc.next();      char arr[]  = userinput.tochararray();     int i;          try {              while ((line = reader1.readline()) != null) {                  (i=0; < arr.length; i++)                 {                    if ((line.indexof(userinput.charat(i)) != -1) && (line.length() == arr.length)) {                        match.add(line);                     }                     else {                 //        system.out.println("no matches");                     }                 }              }             system.out.println(match);         }      catch (ioexception e) {          e.printstacktrace();      } 

**current results: **

words in text file:

cab dog god dogs quick 

user input: "odg"

program output:

[god, god, god, dog, dog, dog] 

the program should return words in dictionary can made out of string entered user managing return both instances in case, however, each displayed 3 times (arr.length).

the standard solution kind of problem is: sort characters of user input. odg become dgo , back become abck. each word in dictionary, same sorting. cab become abc , dog dgo — hey, that’s same first user input, know word should output.

the strong point solution make sure every letter used once. takes duplicate letters account: if same letter comes twice in user input, find words contain letter twice.

if like, can prepare word list in advance building map keys alphabetically sorted words , values lists of words contain same letters. key dgo map list of [dog, god]. have sort input , make lookup.


Comments