Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Hailstone series help

843789May 14 2009 — edited May 16 2009
Hi everyone, I have an assignment to generate the Hailstone series. I have done the programming part, however when executing it an error message pops and I cannot understand or find the nature of it. Please help me if you can. Here's the code:
import java.util.Scanner;

public class Lab5
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner (System.in);
        System.out.print("Would you like to compute a Hailstone series? ");
       
        String a = in.nextLine();
        char answer = a.charAt(0);
       
        while ((answer == 'y') && (a.length() == 1))
        {
            int start = getStartingValue(in);
            int length = computeLength(start);
            int max = computeMax(start);
            outputResults(start, length, max);
           
            System.out.println("");
            System.out.print("Would you like to compute another series? ");
            
            a = in.nextLine();
            answer = a.charAt(0);
        }
       
        if ((a == "n") && (a.length() == 1))
        {
            System.out.println("Goodbye!");
        }
    }
    
    // Given a Scanner in, this method (repeatedly, if necessary)
    // prompts the user to enter a positive integer, inputs the 
    // integer, and if it is positive returns it. Otherwise, is
    // keeps asking the user for a positve integer.
    private static int getStartingValue(Scanner in)
    {
         
        System.out.print("Enter a positive integer: ");
        int start = in.nextInt();
       
        while (start <= 0)
        {
            System.out.print("Enter a positive integer: ");
            start = in.nextInt();           
        }
        return start;
        // Do not declare the Scanner variable in this method.
        // You must use the value this method receives in the
        // argument (in).

    }
    
	// Given a positive starting value x, this method generates the
    // corresponding Hailstone series and returns the length of the
    // series (i.e., how many steps it takes before the series hits 1).
    private static int computeLength(int x)
    {
        int i = 1;
       
        while (x != 1)
        {   
            if (x % 2 == 0)
            {
                x = x / 2;
                i++;
            }
            else if (x % 2 != 0)
            {
                x = (3 * x) + 1;
                i++;
            }
        }
        return i;
    }
    
    // Given a positive starting value x, this method generates the
    // corresponding Hailstone series and returns the maximum value
    // in the series (i.e., the largest value it generates before
    // the series hits 1).
    private static int computeMax(int x)
    {
        int max = x;
       
        while (x != 1)
        {   
            if (x % 2 == 0)
            {
                x = x / 2;
            }
            else if (x % 2 != 0)
            {
                x = (3 * x) + 1;
                if (max < x)
                {
                    max = x;
                }
            }
        }
        return max;
    }
    
    // Given the starting value, the length, and the max value in a
    // Hailstone series, this method outputs them with appropriate
    // messages.
    private static void outputResults(int start, int length, int max)
    {
        System.out.println("");
        System.out.println("Generated series for starting value " + start);
        System.out.println("Series converged in " + length + " steps");
        System.out.println("Largest value generated is " + max);
    }
}
And the output looks like this:

Would you like to compute a Hailstone series? y
Enter a positive integer: 25

Exception in thread "main" Generated series for starting value 25
Series converged in 24 steps
Largest value generated is 88

Would you like to compute another series? java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at Lab5.main(Lab5.java:32)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 13 2009
Added on May 14 2009
6 comments
264 views