1

I have a algorithm to analysis string but it take only about 10000 characters more than that it will throw following exception

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - constant string too long

this is the code I'm using

public static void printRepeatingStrings(String inputString, int sequenceLength) {
    if (inputString.isEmpty() || sequenceLength <= 0 || sequenceLength >= inputString.length()) {
        System.out.println("Invalid input");
    } else {
        int i = 0;
        int j = i + sequenceLength;
        Set<String> tempSet = new HashSet<>();
        Set<String> repeatingSequences = new TreeSet<>();
        while (j <= inputString.length()) {
            if (!tempSet.add(inputString.substring(i, j))) {
                repeatingSequences.add(inputString.substring(i, j));
            }
            i++;
            j = i + sequenceLength;
        }
        for (String str : repeatingSequences) {
            System.out.println(str);
        }
    }
}

If you can help me to fix this that will be very helpful

Nuwan Indika
  • 41
  • 1
  • 3
  • 9

2 Answers2

1

See Java "constant string too long" compile error. Only happens using Ant, not when using Eclipse

there ist stated that the length of a string constant in a class file is limited to 2^16 bytes in UTF-8 encoding.

If you don't use such a long constant, maybe the otpimizing compiler makes some concatenation with static expressions in the main function.

Community
  • 1
  • 1
Turo
  • 4,724
  • 2
  • 14
  • 27
0

You should be able to get a String of length Integer.MAX_VALUE (always 2147483647 (231 - 1) by the Java specification, the maximum size of an array, which the String class uses for internal storage) or half your maximum heap size (since each character is two bytes), whichever is smaller.

Geeth Lochana
  • 164
  • 13