- I should write a CLI through which two Strings can be read in. These two Strings should be checked for whether they are anagrams of each other
- For both Strings, I should give out all their possible anagrams. To avoid repetition, I only give out the anagrams for both of them if they are not already anagrams of each other. Otherwise, I only give out the anagrams for String 1 (since they already include the anagrams for String 2)
My code works without errors (as intended). Now I'm looking on whether I can improve it. Appreciate any help.
Result:
public class Anagram {
static final String SECOND_STRING_INPUT = "You have inserted '%s'. Please insert"
+ " a second String to check whether it's an anagram of '%s'!";
static final String ARE_ANAGRAMS = "\n'%s' and '%s' are anagrams: %b !%n";
private static Set<String> anagramSet;
public static void main(String[] args) {
Scanner inputScan = new Scanner(System.in);
while(true) {
System.out.println("\nPlease enter a String. \nTo end the program, leave "
+ "the input empty and press enter!");
String firstInput = inputScan.nextLine();
if(firstInput.equals("")) {
break;
}
String secondInput = "";
while(secondInput.equals("")) {
System.out.printf(SECOND_STRING_INPUT, firstInput, firstInput);
secondInput = inputScan.nextLine();
if(secondInput.equals("")) {
System.out.println("The input at this point can not be blank. "
+ "Please provide a non-blank input.");
}
}
performEvaluation(firstInput, secondInput);
System.out.println("\n---------new evaluation begins here----------");
}
System.out.println("The program's execution has been ended!");
inputScan.close();
}
private static void performEvaluation(String firstInput, String secondInput) {
boolean areAnagrams = areAnagrams(firstInput, secondInput);
System.out.printf(ARE_ANAGRAMS, firstInput, secondInput, areAnagrams);
if(firstInput.replace(" ","").length() <= 10) {
System.out.println(allAnagramMessages(areAnagrams, firstInput, secondInput));
printAnagrams(firstInput.toLowerCase().replace(" ",""));
}else {
System.out.println("The anagrams will not be printed, since the Strings "
+ "were logner than ten letters!");
}
if(!areAnagrams && !firstInput.equals(secondInput)
&& secondInput.replace(" ","").length() <= 10) {
System.out.printf("%nThe possible anagrams of '%s' are: %n", secondInput);
printAnagrams(secondInput);
}
}
private static void printAnagrams(String i_Word) {
i_Word = stripDownString(i_Word);
anagramSet = new HashSet<>();
findAnagrams(i_Word);
anagramSet.forEach(System.out::println);
}
public static boolean areAnagrams(String firstInput, String secondInput) {
String firstInputMod = stripDownString(firstInput);
String secondInputMod = stripDownString(secondInput);
if(firstInputMod.length() != secondInputMod.length()) {
return false;
}else if(firstInputMod.equals(secondInputMod)) {
return false;
}else{
return disassembleToLetters(firstInputMod)
.equals(disassembleToLetters(secondInputMod));
}
}
private static Map<String,Integer> disassembleToLetters(String i_word){
Map<String,Integer> resultMap = new HashMap<>();
stringToList(i_word)
.forEach(lttr -> resultMap.put(lttr, resultMap.getOrDefault(lttr, 0)));
return resultMap;
}
private static String allAnagramMessages(boolean areAnagrams, String firstInput,
String secondInput) {
StringBuilder sb = new StringBuilder();
sb.append(String.format("%nThe possible anagrams of '%s' ", firstInput));
if(areAnagrams) {
sb.append(String.format("and '%s' ", secondInput));
}
sb.append("are:");
return sb.toString();
}
public static void findAnagrams(String i_Word) {
String wordStripped = stripDownString(i_Word);
if(wordStripped.length() == 1) {
anagramSet.add(wordStripped);
return;
}
stringToList(wordStripped).stream().forEach(lttr -> {
List<String> restList = new ArrayList<>(stringToList(wordStripped));
restList.remove(lttr);
findAnagrams(new StringBuilder(lttr), restList);
});
}
public static void findAnagrams(StringBuilder currString, List<String> restList) {
if(restList.size() == 1) {
StringBuilder currStringCopy = new StringBuilder(currString);
anagramSet.add(currStringCopy.append(restList.get(0)).toString());
}else {
restList.stream().forEach(lttr -> {
StringBuilder currStringCopy = new StringBuilder(currString);
currStringCopy.append(lttr);
List<String> restListCopy = new ArrayList<>(restList);
restListCopy.remove(lttr);
findAnagrams(currStringCopy, restListCopy);
});
}
}
private static String stripDownString(String i_Word) {
return i_Word.replace(" ","").toLowerCase();
}
public static Set<String> getAnagramSet(){
return anagramSet;
}
public static void setAnagramSet(Set<String> i_Set){
anagramSet = i_Set;
}
}
stringToList()
is missing \$\endgroup\$