1

I created a simple program to read from a list some values and keep only those of a specified length and also to extract 3 characters from them, but from a different position each time.

For example if we have the value nick07n the program should extract three random characters from it that might be the same but not taken from the same position:

n0n [0, 4, 6]

While the program runs correctly (as I've seen some results), the whole procedure is not performed successfully until the end or not performed at all, as I receive the following memory error:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.util.Arrays.copyOf(Unknown Source)
at java.util.ArrayList.grow(Unknown Source)
at java.util.ArrayList.ensureExplicitCapacity(Unknown Source)
at java.util.ArrayList.ensureCapacityInternal(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
at guessingTool.CheckProgram.main(CheckProgram.java:103)

Here is the code I have so far:

for (int i = 0; i < 5; i++) {

        index = randomGenerator.nextInt(valuesList.size());
        value = valuesList.get(index);

        finalValue = "";
        positions.clear();
        for (int j = 0; j < 3; j++) {
        index = randomGenerator.nextInt(value.length());
        valueChar = value.charAt(index);
        positions.add(index);

        if (j == 0) {
            finalValue += Character.toString(valueChar);
        }
        else if (j == 1) {
            if (positions.get(0) != positions.get(1)) {
                finalValue += Character.toString(valueChar);
            }
            else {
                while (positions.get(0) == positions.get(1)) {
                    index = randomGenerator.nextInt(value.length());
                    valueChar = value.charAt(index);
                    positions.add(index);
                }
            }
        }
        else if (j == 2) {
            if (positions.get(0) != positions.get(2) && positions.get(1) != positions.get(2)) {
                finalValue += Character.toString(valueChar);
            }
            else {
                while (positions.get(0) == positions.get(2) || positions.get(1) == positions.get(2)) {
                    index = randomGenerator.nextInt(passcode.length());
                    valueChar = value.charAt(index);
                    positions.add(index);
                }
            }
        }

        }
        System.out.println(finalValue + " " + positions);
}

Is there any optimization that could happen or any other suggested approach?

Thanks in advance.

John Stef
  • 585
  • 1
  • 4
  • 16
  • When your code gets inside `while` loops it seems it will stay there until your memory is exhausted. – PM 77-1 Feb 26 '17 at 17:04
  • This is what I thought, but is this due to an error in code that causes an infinite loop or it just consumes so much memory in order to find the correct index that satisfies the condition? – John Stef Feb 26 '17 at 17:06
  • You certainly have an error in your code. Debug your last while-loop in the else if (j == 2) block. I'd guess that this loop runs infinite. I can't debug it myself as you did not post the entire code including the needed variables and imports. –  Feb 26 '17 at 17:08

2 Answers2

2

I think your problem comes from part of your code that looks like this:

  while (positions.get(0) == positions.get(1)) {
                    index = randomGenerator.nextInt(value.length());
                    valueChar = value.charAt(index);
                    positions.add(index);
  }

What happens if the condition is true, is that you append at the end of your positions list the index (positions.add(index) does not push the index, it merely appends at the end of the list), but the condition still holds true, so you have an infinite loop there, adding more and more elements in your list, and at some point you don't have enough heap to hold all of them. See Javadoc https://docs.oracle.com/javase/7/docs/api/java/util/List.html#add(E)

Adonis
  • 4,670
  • 3
  • 37
  • 57
1

I finally managed to find the error in my code. Thank you all for your answers, they helped me a lot. The problem was inside the while loop as everyone mentioned.

I changed this:

else {
      while (positions.get(0) == positions.get(1)) {
            index = randomGenerator.nextInt(value.length());
            valueChar = value.charAt(index);
            positions.add(index);
      }
}    

To this:

else {
     while (positions.get(0) == index) {
            index = randomGenerator.nextInt(value.length());
     }
     valueChar = value.charAt(index);
     positions.add(index);
     finalValue += Character.toString(valueChar);
}

Now the program works correctly.

John Stef
  • 585
  • 1
  • 4
  • 16