Hailstone Sequence

i'm currently working on the hailstone sequence program and i keep receiving this error "Exception in the thread "main" java.lang.outOfMemoryError : java heap space"

i have no idea how to fix it...plz help

here is the code

import java.util.ArrayList;

import java.util.Scanner;

public class Hailstone

{

public static ArrayList<Integer> findHailStone ( int seed ) // RETURN method

{

ArrayList list = new ArrayList();

list.add(seed);

while(seed != 1)

{

if(seed%2 == 0)

{

seed = seed/2;

list.add(seed);

}

else

{

seed = (seed*3)-1;

list.add(seed);

}

}

return list;

}

public static int findHighestValueInSequence ( ArrayList [] temp )

{

int high = 0;

for(int i=0; i<temp.length; i++)

{

for(int j=0; j><temp.size(); j++)

{

if(high >< ((Integer)temp.get(j)).intValue())

high = ((Integer)temp.get(j)).intValue();

}

}

return high;

}

public static void main(String [] args)

{

Scanner reader = new Scanner(System.in);

ArrayList [] list = new ArrayList[1000];

System.out.print("Input a number to find the hailstone" +

" sequence from 0 to that number: ");

int k = reader.nextInt();

for(int j=0; j<=k; j++)

{

list[j]=(findHailStone(j));

}

System.out.print("\nThe highest value in sequence is " +

findHighestValueInSequence(list));

for(int i=5; i<11; i++)

{

System.out.println(findHailStone(i));

}

}

}

[1707 byte] By [pita8912@hotmail.coma] at [2007-11-27 4:54:39]
# 1

First of all, welcome to the forum!

Second, please learn to use code tags. More people will read your code if it is formated nicely.

Third, I think your code is missing some elements. In the section below, I added a few "[ i ]"'s.

for (int i = 0; i < temp.length; i++)

{

for (int j = 0; j > temp[i].size(); j++)

{

if (high > ((Integer) temp[i].get(j)).intValue())

high = ((Integer) temp[i].get(j)).intValue();

}

}

4th: Could it be that you are starting at 0, and your algorithm checks for seed != 1. When it starts with 0, it will keep dividing it by 2 and adding it to the list til you run out of heap.

Message was edited by:

petes1234

petes1234a at 2007-7-12 10:09:25 > top of Java-index,Java Essentials,Java Programming...
# 2

There may be other problems w/ your code, but the most potentially pressing is I think that your algorithm is wrong. You have

else

{

seed = (seed * 3) - 1;

list.add(seed);

}

and shouldn't it be:

else

{

seed = (seed * 3) + 1;

list.add(seed);

}

Otherwise you'll run into an infinite loop of 20 - 10 - 5 - 14 - 7 - 20 - ...

I believe that this sequence is supposed to end eventually for all numbers, not go into an infinite loop.

Good luck!

/Pete

petes1234a at 2007-7-12 10:09:25 > top of Java-index,Java Essentials,Java Programming...
# 3

Enclosing the Java code in [code][/code] tags will stop [i] being parsed as a formatting tag, but it won't fix the other problem. Wherever there's a '<' (less-than sign) in yor post, you should either make sure it's followed by a space, or replace it with the &lt; escape. Otherwise, the buggy forum software is likely to add greater-than signs ('>') at seemingly random points in your post.

uncle_alicea at 2007-7-12 10:09:25 > top of Java-index,Java Essentials,Java Programming...
# 4
> Enclosing the Java code in [code][/code] tags> will stop [i] being parsed as a formatting> tag, but it won't fix the other problem. Ah, so that's why the [ i ]'s were lost! Sorry about that. And thanks for educating me "uncle".
petes1234a at 2007-7-12 10:09:25 > top of Java-index,Java Essentials,Java Programming...