Is There A problem With My Mnemonics Recursion Code? It Crashes as Line 1 Trying to print Output

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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> 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;
}
}
}
</code>
<code> 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; } } } </code>
        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.

New contributor

Kent Gatera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật