Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

What's the significance of random name generator Java?

Asked by Amit Raj Oct 11, 2022 855 views 1 answer
Share

About this question

 wrote a working random name generator in Java. Here's my code:


NameGenerator.java:


package pl.hubot.dev.name Generator;


import java.util.*;
import java.util.stream.Collectors;
public class NameGenerator {
    private static final String CHARACTERS = "abcdefghijklmnopqrstuvwxyz";
    private static Random random = new Random();
    void generate() {
        WordChecker wordChecker = new WordChecker();
        StringSimilarity stringSimilarity = new StringSimilarity();
        Dictionary dictionary = new Dictionary();
        final String filepath = "/usr/share/dict/american-english";
        dictionary.build(filepath);
        List syllabes = combination(CHARACTERS, 3).parallelStream()
                .filter((x) -> wordChecker.countSyllables(x) > 1).collect(Collectors.toList());
        String word = "";
        while (stringSimilarity.similarity(word, dictionary.getRandomEntries(1)[0]) < 0>            word = syllables.get(random.nextInt(syllabes.size())) + syllabes.get(random.nextInt(syllabes.size()));
        }
        System.out.println(word);
    }
    private Set combination(String input, int n) {
        List letters = new ArrayList();
        for (int i = 0; i < input.length(); ++i)
            letters.add(input.charAt(i));
        Set results = new HashSet();
        combination("", letters, n, results);
        return results;
    }
    private void combination(String soFar, List letters, int n,
                            Set results) {
        if (n == 0) {
            results.add(soFar);
            return;
        }
        int startIndex = soFar.length();
        if (startIndex >= letters.size())
            return;
        for (int i = startIndex; i < letters>            // ch is the next candidate to add to the result that we're
            // building (soFar)
            char ch = letters.get(i);
            // Swap the characters at positions i and position startIndex.
            char temp = letters.get(startIndex);
            letters.set(i, temp);
            letters.set(startIndex, ch);
            // add ch to soFar, compute combinations of length n-1.
            // as startIndex is essentially soFar.length() this means that
            // the recursive call will only process the remainder of the list.
            combination(soFar + ch, letters, n - 1, results);
            // Swap the characters back - restore the original state.
            letters.set(i, ch);
            letters.set(startIndex, temp);
        }
    }
}

Could I ask about reviewing this code?

Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.