-1

I need to write a code for a bad chemistry example, so when i try to separate the string of a compound into elements with the numbers next to them, using an array, it returns this error. Note that the "jelibroj" and "element" are methods that I didnt include in the post, but they all work.

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)

And here is the code.

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc=new Scanner(System.in);
    String s= sc.nextLine();
    s=s.replace("+", " ");
    String [] jedinjenja = new String [(s.split(" ").length)];
    jedinjenja = s.split(" ");
    ArrayList<String> elementi = new ArrayList<String>();
    int poc=0;
    for (int i=0; i<jedinjenja.length; i++){
        for (int j=0;j<jedinjenja[i].length();j++){
            if(j>1){
                if(jelibroj(jedinjenja[i].charAt(j))==true && element(jedinjenja[i].charAt(j-3))==true){
                    elementi.add(jedinjenja[i].substring(poc, j-2));
                    elementi.add(jedinjenja[i].substring(poc+1, j));
                    j=poc;
                } else if(jelibroj(jedinjenja[i].charAt(j))==true && element(jedinjenja[i].charAt(j-3))==false){
                    elementi.add(jedinjenja[i].substring(poc, j));  
                    j=poc;
                }
            }
            if(jelibroj(jedinjenja[i].charAt(j))==true){
                elementi.add(jedinjenja[i].substring(poc, j));  
                j=poc;
            }
        }
    }
    String[] stockArr = new String[elementi.size()];
    stockArr = elementi.toArray(stockArr);

    for(int i=0;i<stockArr.length;i++){
        System.out.println(stockArr[i]);
    }
}
Titus
  • 22,031
  • 1
  • 23
  • 33
siozd23
  • 13
  • 5
  • Can you give an example of an input and an expected output so we know what we should be expecting to see? – KM529 Jan 19 '18 at 19:14
  • As you can see from the stack trace that Arrays.copyOf() is throwing this error and there is no Arrays.copy inside main method. So I presume the issue must be inside the other two methods. Can you please provide the full stack trace. – Neelesh Jan 19 '18 at 19:20
  • @Neelesh, Array::add is being called, and ArrayList is expanding, and in doing so calls copyOf(). He calls elementi.add(), and elementi is a ArrayList. – Steve Brandli Jan 19 '18 at 19:22
  • @KM529 This isnt the final code, but hopefully this part of code would have an input of C2HS3+Al3C4 and split it up into C2, H, S3 etc. However I am a begginer so this might not work like at all. – siozd23 Jan 20 '18 at 16:20

1 Answers1

1

I notice that, in three places, you:

j = poc;

where poc is 0. Yet nowhere in the loop do you set poc. This would have the effect of creating an unending loop until you run out of memory.

A session with a debugger may have been helpful here. I can't count the number of times that I have stared at code, missing an obvious problem, and found it with the appropriate slap to the forehead using a debugger.

Steve Brandli
  • 556
  • 4
  • 14
  • As you can see set the poc before the loop, thought that would do the trick. Is there something wrong with that or? – siozd23 Jan 20 '18 at 16:23
  • The problem is not that poc is undefined. Yes, you set it to zero before the loop. Then, within the j loop, you set j to poc (which is zero) in nearly each iteration. So j does not progress and the loop never terminates. – Steve Brandli Jan 20 '18 at 16:43
  • Thanks so much man, I meant to put it in reverse, so poc=j, which now works. – siozd23 Jan 20 '18 at 20:51