By Denise


2019-06-12 07:19:42 8 Comments

I am getting the following error while trying to execute this code , and i cannot figure out why. It seems to be related to using StringBuffer and appending to it.

Code :

class Stock {

    static String findStock(int[] arr) {
        int flag = 0;
        int begin = 0;
        int start = 0;
        int endIndex = 0;

        boolean foundStart = false;

        StringBuffer op = new StringBuffer();

        while (flag != 1) {

            //find start
            for (int i = begin; i < arr.length - 1; i++) {
                if (arr[i + 1] > arr[i]) {
                    start = arr[i];
                    endIndex = i;
                    foundStart = true;
                    break;
                }
                if (i == arr.length - 1) {
                    flag = 1;
                }
            }
            //find string

            while (arr[endIndex + 1] > arr[endIndex]) {
                endIndex++;
                if (endIndex == arr.length - 1) break;
            }
            int end = arr[endIndex];
            op.append("()");

        }
        return op.toString();
    }

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

        int n = sc.nextInt();
        int[] arr = new int[n];

        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        String output = findStock(arr);
        System.out.println(output);
    }
}

Error:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:3332)
    at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:124)
    at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:448)
    at java.lang.StringBuffer.append(StringBuffer.java:270)
    at Stock.findStock(Stock.java:43)
    at Stock.main(Stock.java:61)

1 comments

@Arnaud 2019-06-12 07:23:10

Your whileloop will never end, since flag will never be 1.

See the condition in the for loop : i<arr.length-1 .

i==arr.length-1 will never be true.

Your StringBuffer will keep appending things without limit, until you get a OutOfMemoryError .

for(int i=begin; i<arr.length-1; i++){
...
 if(i==arr.length-1){
    flag = 1;
 } 

}

@Denise 2019-06-12 07:53:34

Got it ...thankyou !

Related Questions

Sponsored Content

42 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

34 Answered Questions

[SOLVED] How to split a string in Java

  • 2010-08-14 03:01:53
  • riyana
  • 3652514 View
  • 1532 Score
  • 34 Answer
  • Tags:   java string

58 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

21 Answered Questions

[SOLVED] How do I call one constructor from another in Java?

  • 2008-11-12 20:10:19
  • ashokgelal
  • 788167 View
  • 2102 Score
  • 21 Answer
  • Tags:   java constructor

40 Answered Questions

[SOLVED] How do I efficiently iterate over each entry in a Java Map?

26 Answered Questions

[SOLVED] How do I determine whether an array contains a particular value in Java?

  • 2009-07-15 00:03:21
  • Mike Sickler
  • 1850979 View
  • 2155 Score
  • 26 Answer
  • Tags:   java arrays

65 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 3852297 View
  • 3302 Score
  • 65 Answer
  • Tags:   java random integer

26 Answered Questions

[SOLVED] How to get an enum value from a string value in Java?

  • 2009-03-02 22:56:34
  • Malachi
  • 1032130 View
  • 1858 Score
  • 26 Answer
  • Tags:   java enums

3 Answered Questions

[SOLVED] Can logging a huge message (around 90 MB) cause OutOfMemory Error? (Max heap size : 2048M)

  • 2016-07-06 08:15:17
  • Sreeja Mohan
  • 601 View
  • 0 Score
  • 3 Answer
  • Tags:   java

0 Answered Questions

Java heap space error for stringBuilder object

Sponsored Content