My Recursion program I made Should work , I want to print to console before adding the File Writer part but maybe I am missing something simple. I would Appreciate any help I can get, I am new here, so forgive any Formatting errors I made, feedback is welcome. Thank you.
- Explanation:
For Each Letter in the Mnemonics Array Assigned to an Input number like:”623″ the program will recursively call itself and combine different letters return, a combo, and add it back to the ‘prefix’ letter it was called for.
import java.util.*;
import java.io.*;
/**
* Uses recursion to make Mnemoics.
*
* @author K.Dot
* @version 1.0
* @since 2024-05-06
*/
// MnemonicsCombos class
public final class Factorial {
/** Private constructor to prevent instantiation.
* @return */
private void MnemonicsCombos() {
throw new UnsupportedOperationException("Cannot instantiate");
}
public static void main(final String[] args) {
final File inputFile = new File("input.txt");
final String outputFile = "output.txt";
ArrayList<String> MnemonicsOutputList = new ArrayList();
/*
* - This is where the dictionary, for Mnemonics goes.
* - This is where the call for Mnemonics go.
*/
Map<String, String> lettersForDigitDictionary = new HashMap<>();
lettersForDigitDictionary.put("1", "1");
lettersForDigitDictionary.put("2", "ABC");
lettersForDigitDictionary.put("3", "DEF");
lettersForDigitDictionary.put("4", "GHI");
lettersForDigitDictionary.put("5", "JKL");
lettersForDigitDictionary.put("6", "MNO");
lettersForDigitDictionary.put("7", "PQRS");
lettersForDigitDictionary.put("8", "TUV");
lettersForDigitDictionary.put("9", "WXYZ");
lettersForDigitDictionary.put("0", " ");
try {
Scanner sc = new Scanner(inputFile);
FileWriter fileWriter = new FileWriter(outputFile);
BufferedWriter writer = new BufferedWriter(fileWriter);
while(sc.hasNextLine()){
int ErrorlineIter = 1;
try{
String mnemonicNumAsStr = sc.nextLine();
MnemonicsOutputList = ListAllMnemonics(mnemonicNumAsStr, MnemonicsOutputList, lettersForDigitDictionary);
String[] MnemonicsOutputArray = MnemonicsOutputList.toArray(new String[0]);
for (String combo : MnemonicsOutputArray) {
System.out.print(combo + " ");
}
}catch(Exception e){
System.out.println("Error at line: " + ErrorlineIter);
}
ErrorlineIter++;
}
System.out.println("Done.");
writer.close();
sc.close();
} catch (Exception e) {
System.out.println("Invalid input path!");
}
}
/**
* Recursive method that gets the MnemonicsCombos of a number.
*
* @param someNumString is the 3 letter umber we send the console.
* @param MnemonicsCombos is the combination we get back.
* @param AlphaNumMap is the dictionary that stores the map
*/
private static ArrayList ListAllMnemonics(String someNumString, ArrayList<String> MnemonicsCombos, Map<String, String> alphNumMap) {
if (someNumString.length() == 0){
return MnemonicsCombos;
}
else {
// We do the recursive Call case.
char currentNumFromString = someNumString.charAt(0);
String dictDefinition = alphNumMap.get(currentNumFromString);
//For each letter in the dict-def. Which will be known as prefix. (See next line)
for (char frontLetter : dictDefinition.toCharArray()) {
//Call funtion to get suffix cominations and assign them to to each prefix letter.
// We Assign it to a temporary list called "listOfSuffixes".
List<String> listOfSuffixes = ListAllMnemonics(someNumString.substring(1), MnemonicsCombos, alphNumMap);
for (String suffixCombination : listOfSuffixes) {
// The (prefix + suffix) = "newValue" I will assign to Mnemonics to return it.
String newValue = frontLetter + suffixCombination;
MnemonicsCombos.add(newValue);
}
}
return MnemonicsCombos;
}
}
}
I can’t say I tried much, I am utterly stumped as to why it is crashing, chat GPT could not be of any help.
Kent Gatera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.