I am working on a question from an assignment. It is called the 3n+1 problem. The task is to get an integer from user (not 1 or negative number) and based on the number input (n), if it is even - divide by 2, and if it is negative - multiple n * 3 + 1.
The method I MUST use is as follows:
public static ArrayList<Integer> getHailstoneSequence(int n) {
^ this part is mandatory for my assignment, so it is necessary for me to work with an ArrayList of Integers.
I am struggling to make my program work. I can't figure out if I should store the input in the main method OR in my definition class. I also am not sure how to have the loop execute for even numbers in the main method without the redundancy of already having it stated in the definition class ( where my getHailstoneSequence() method is located).
Here is the code I have: (DEFINITION CLASS)
package question7;
import java.util.ArrayList;
public class HailstoneSequence {
// Method for computing the Hailstone Sequence:
public static ArrayList<Integer> getHailstoneSequence(int n) {
ArrayList<Integer> hailstoneSequence = new ArrayList<Integer>();
if (n % 2 == 0) {
n = n / 2;
hailstoneSequence.add(n);
}
else {
n = (n * 3) + 1;
hailstoneSequence.add(n);
}
return hailstoneSequence;
}
}
I am unsure how to include the method I created above into the main method for printing. I want the output to look like this (example):
5 is odd, so we make 3N+1: 16 16 is even, so we take half: 8 8 is even, so we take half: 4 4 is even, so we take half: 2 2 is even, so we take half: 1
And have the program stop whe n = 1
Here is what I have in my main method to date:
package question7;
import java.util.ArrayList;
import java.util.Scanner;
public class HailstoneSequenceTest {
public static void main(String[] args) {
Scanner hailstone = new Scanner(System.in);
System.out.println("To begin, please enter a positive integer that is not 1:");
int n = hailstone.nextInt();
ArrayList<Integer> list = HailstoneSequence.getHailstoneSequence(n);
for (int sequence : list) {
try {
if (n > 1) {
System.out.println("Great choice! Let's begin!");
System.out.println();
while (n % 2 == 0) {
System.out.println(n +
" is even, so we take half: " +
HailstoneSequence.getHailstoneSequence(n));
list.add(n);
if (n == 1) break;
while (n % 2 != 0) {
System.out.println(n +
" is odd, so I make 3n+1: " +
HailstoneSequence.getHailstoneSequence(n));
list.add(n);
if (n == 1) break;
}
// while (n == 1) {
// System.out.println(sequence);
// break;
}
}
}
catch (Exception error) {
while (n <= 1) {
System.out
.println("You did not enter a valid positive, greater than 1 integer. Please try again: ");
System.out.println();
n = hailstone.nextInt();
}
// End of HailstoneSequenceTest class
hailstone.close();
}
}
}
}
// }
Does anyone have any idea where I am going wrong? I know my code is probably wrong if multiple ways, I am just not sure where to start.
Do I need a for loop to hold the characteristics and increment ect.. ?
When I try to do this the way I know, it says I must return a ArrayList not an Int .
Please advise.


getHailstoneSequence()and returning a list is supposed to return a list of a single element.