String Generator code (School assignment)

Question:
I have been trying to have this code right without any success. Here is what I am suppose to code: ‘A Java program that displays all 4-character strings on the screen in lexicographical order (also known as dictionary order). It must display one 4-character string per line. Only the characters a, b, c, d are allowed within strings, but you can reuse the same character more than once within the same string. Within the same string, the ‘b’ character must always be immediately followed by the ‘a’ character. The same string cannot contain both the ‘d’ character and the ‘a’ character. Your program should end with the number of strings displayed on the screen.’

Repl link:
https://replit.com/@imanovskichatto/ChaineLexicojava

class Main {
  public static void main(String[] args) {
    int count = 0;
    char[] charSet = { 'a', 'b', 'c', 'd' };
    for (char c1 : charSet) {
      for (char c2 : charSet) {
        for (char c3 : charSet) {
          for (char c4 : charSet) {
            if (c1 == 'b' && c2 == 'a' && c3 != 'd' && c4 != 'a') {
              System.out.println("" + c1 + c2 + c3 + c4);
              count++;
            }
          }
        }
      }
    }
    System.out.println("Nombre total de chaînes: " + count);
  }
}

Hi @imanovskichatto!

Is this a school assignment?

Hi, yes it is and i ve been trying since sunday without success i ve tried quite a few imbrication and different conditions but still not getting the result i need.

Oh right!

First:

So the rules for your string generation are:

  1. Strings are 4 characters long.
  2. Only the characters ‘a’, ‘b’, ‘c’, and ‘d’ are allowed.
  3. ‘b’ must always be followed by ‘a’.
  4. A string cannot contain both ‘d’ and ‘a’.

So, picking up your code:

public static void main(String[] args) {
    int count = 0;
    char[] charSet = { 'a', 'b', 'c', 'd' };

    // You loop through each character for the first position (ok)
    for (char c1 : charSet) {
      // Loop through each character for the second position (ok too)
      for (char c2 : charSet) {
        // Loop through each character for the third position (also ok)
        for (char c3 : charSet) {
          // Loop through each character for the fourth position (ok too)
          for (char c4 : charSet) {
            //  But here is the problem, your condition only checks for 'b' followed by 'a' in the first two positions
            // and excludes 'd' and 'a' in the third and fourth positions, respectively.
            // You need to check if 'b' is followed immediately by 'a' in any part of the string.
            // Also, you should ensure that 'a' and 'd' are not present in the same string.
            // Another thing is that your check does not handle all of the conditions correctly.
            if (c1 == 'b' && c2 == 'a' && c3 != 'd' && c4 != 'a') {
              System.out.println("" + c1 + c2 + c3 + c4);
              count++;
            }
          }
        }
      }
    }
    // Print the total number of strings generated.
    System.out.println("Nombre total de chaînes: " + count);
  }
}

Remember that you must:

Following ‘b’ with ‘a’:

  • For any ‘b’ in the string, you must ensure the next character is ‘a’. This means that if the current loop character is ‘b’, you should only proceed if the next character in sequence is ‘a’. This also implies that ‘b’ cannot be at the last position of your string since it would have no following character.
  • You can’t just check the next character immediately since you’re in a loop. You’ll need to remember the position of ‘b’ and check the subsequent position once you’re there.

Don’t forget that you need to be excluding ‘a’ and ‘d’ together:

  • You will need to check the entire string to make sure ‘a’ and ‘d’ do not coexist. This means if ‘a’ is found anywhere in the string, ‘d’ cannot be present, and vice versa.
  • You can handle this by using a flag or a boolean variable that you set to true if you encounter an ‘a’ and then for every subsequent character you check if ‘d’ is present when this flag is true.

For example:

// You can create boolean flags for conditions
boolean hasA = c1 == 'a' || c2 == 'a' || c3 == 'a' || c4 == 'a';
boolean hasD = c1 == 'd' || c2 == 'd' || c3 == 'd' || c4 == 'd';

// And make sure 'a' and 'd' are not in the same string
boolean notBothAD = !(hasA && hasD);

// this is not the solution just a tip to help you build the code

3 Likes