Trying to create a program that outputs all possible combinations of the letters A-D

Viewed 158

I'm trying to write a program that takes user input for a positive integer wordLength and lists all possible arrangements of wordLength capital letters between A and D. The code I'm using now should output

AA
AB
AC
AD
BA
BB
BC
BD 

and so on when the input is 2. However, this is the output I'm getting:

AA
AAAB
AAABAC
AAABACAD
AAABACADBA
AAABACADBABB
AAABACADBABBBC
AAABACADBABBBCBD
AAABACADBABBBCBDCA
AAABACADBABBBCBDCACB
AAABACADBABBBCBDCACBCC
AAABACADBABBBCBDCACBCCCD
AAABACADBABBBCBDCACBCCCDDA
AAABACADBABBBCBDCACBCCCDDADB
AAABACADBABBBCBDCACBCCCDDADBDC
AAABACADBABBBCBDCACBCCCDDADBDCDD
AA
AAAB
AAABAC
AAABACAD
AAABACADBA
AAABACADBABB
AAABACADBABBBC
AAABACADBABBBCBD
AAABACADBABBBCBDCA
AAABACADBABBBCBDCACB
AAABACADBABBBCBDCACBCC
AAABACADBABBBCBDCACBCCCD
AAABACADBABBBCBDCACBCCCDDA
AAABACADBABBBCBDCACBCCCDDADB
AAABACADBABBBCBDCACBCCCDDADBDC
AAABACADBABBBCBDCACBCCCDDADBDCDD
AA
AAAB
AAABAC
AAABACAD
AAABACADBA
AAABACADBABB
AAABACADBABBBC
AAABACADBABBBCBD
AAABACADBABBBCBDCA
AAABACADBABBBCBDCACB
AAABACADBABBBCBDCACBCC
AAABACADBABBBCBDCACBCCCD
AAABACADBABBBCBDCACBCCCDDA
AAABACADBABBBCBDCACBCCCDDADB
AAABACADBABBBCBDCACBCCCDDADBDC
AAABACADBABBBCBDCACBCCCDDADBDCDD

Edit: I'm so sorry for how the output looks, I can't get it to format correctly. The code is as follows:

import java.util.Scanner;

public class WordGenerator {
    public static void main(String [] args) {
        Scanner scnr = new Scanner(System.in);

        System.out.print("Enter desired 'word' length: ");
        int wordLength = scnr.nextInt();
        System.out.println(wordLength);

        String s = "";
        int i = 0;
        char letter1;
        char letter2;
        char letter3;
        char letter4;

        if (wordLength == 1) {
            letter1 = 'A';
            while (letter1 <= 'D') {
            letter1++;
            s = s + letter1;
            System.out.println(s);
            }   
        }
        else if(wordLength% 2 == 0){
            while (s.length() <= wordLength) {
                letter1 = 'A';
                while (letter1 <= 'D') {
                    letter2 = 'A';
                    while (letter2 <= 'D') {
                        s = s + letter1 + letter2;
                        System.out.println(s);
                        letter2++;
                }
                letter1++;
            }
            wordLength--;
            
            }
        }
            else if(wordLength % 2 != 0) {

                while (s.length() <= wordLength) {
                letter1 = 'A';
                while (letter1 <= 'D') {
                    letter2 = 'A';
                    while (letter2 <= 'D') {
                        s = s + letter1 + letter2;
                        letter2++;
                }
                letter1++;
            }
                letter3 = 'A';
                while (letter3 <= 'D') {
                    letter3++;
                    s = s + letter3;
                    System.out.println(s);
            }   
                
                wordLength--;
            }
        }   
    
}
}
2 Answers

Replace

 s = s + letter1 + letter2

With

 s = "" + letter1 + letter2

The problem is that you continue to concatenate all the values to the s variable without to reset it.

You could also remove the first while loop while (s.length() <= wordLength) I don't see the need to have it.

You have four characters, the letters A to D. For each position you have four possible symbols. So for words of length one there are 4 combinations. For words of length two 4*4 combinations, for words of length three 4*4*4 combinations and so on. So you do nothing else but go through all the numbers on base 4. Only instead of 0,1,2,3 you have A,B,C,D. To output the desired result, just calculate 4^wordLength and go from 0 to this result. Convert each number in this range to its base 4 representation. Replace 0 with A, 1 with B, 2 with C and 3 with D

import java.util.Scanner;

public class NewClass {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);

        System.out.print("Enter desired 'word' length: ");
        int wordLength = scnr.nextInt();

        for (int i = 0; i < upperLimit(wordLength); i++){
            String base4 = String.format("%"+wordLength+"s", Integer.toString(i,4)).replace(' ','0');
            String out   = base4.replace('0','A').replace('1','B').replace('2','C').replace('3','D');
            System.out.println(out);
        }
    }

    public static int upperLimit(int x){
        return (int) Math.pow(4, x);
    } 
}
Related