title: How to return an ArrayList <Integer>?
body: <p>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.</p>
  
  <p>The method I MUST use is as follows:</p>
  
  <pre><code>public static ArrayList&lt;Integer&gt; getHailstoneSequence(int n) {
  </code></pre>
  
  <p>^ this part is mandatory for my assignment, so it is necessary for me to work with an ArrayList of Integers. </p>
  
  <p>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).</p>
  
  <p>Here is the code I have: <strong>(DEFINITION CLASS)</strong></p>
  
  <pre><code>package question7;
  
  import java.util.ArrayList;
  
  public class HailstoneSequence {
  
      // Method for computing the Hailstone Sequence:
  
      public static ArrayList&lt;Integer&gt; getHailstoneSequence(int n) {
  
          ArrayList&lt;Integer&gt; hailstoneSequence = new ArrayList&lt;Integer&gt;();
  
              if (n % 2 == 0) {
  
                  n = n / 2;
  
                  hailstoneSequence.add(n);
              }
              else {
                  n = (n * 3) + 1;
                  hailstoneSequence.add(n);
              }
              return hailstoneSequence;
      }
   }
  </code></pre>
  
  <p>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):</p>
  
  <p>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</p>
  
  <p>And have the program stop whe n = 1</p>
  
  <p>Here is what I have in my <strong>main method</strong> to date:</p>
  
  <pre><code>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&lt;Integer&gt; list = HailstoneSequence.getHailstoneSequence(n);
  
          for (int sequence : list) {
              try {
                  if (n &gt; 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 &lt;= 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();
  
              }
  
          }
      }
  }
  
  // }
  </code></pre>
  
  <p>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.</p>
  
  <p>Do I need a for loop to hold the characteristics and increment ect.. ?</p>
  
  <p>When I try to do this the way I know, it says I must return a ArrayList not an Int .</p>
  
  <p>Please advise. </p>