Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: My while loop has run into an infinite loop...?

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default My while loop has run into an infinite loop...?

    I have to write a program that computes a close enough value for the half-life of carbon-14 using comparison of floating point numbers. The while loop should be controlled by a comparison of half of the initial amount and the computed amount remaining. I'm confused as to how to get the number of years because my while loop becomes infinite and I dont know how to stop it. The value should be around 5000 years. Thanks for any help, I really appreciate it!

    public class Lab321
    {
      public static void main(String[] args)
      {
        final double EPSILON = 1.0E-7;
        final double DECAY = 1.14E-4;
     
        double initialAmount = 2.0E-5;
        double computedAmount = initialAmount - (DECAY / 100) * initialAmount;
        int year = 0;
     
        while (Math.abs(computedAmount - 1.0E-5) > EPSILON)
        {
          year++;
          double amountRemaining = initialAmount - (DECAY / 100) * initialAmount;
          System.out.println(amountRemaining);
        }
        System.out.println("The approximate value for the half life of carbon-14 is " + year + " years.");
      }
    }
    Last edited by kari4848; February 28th, 2011 at 10:11 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: My while loop has run into an infinite loop...?

    At no point in your loop are you modifying the value of computedAmount. Also, even if you replaced amountRemaining with computedAmount, the value you're assigning is an unchanging value.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: My while loop has run into an infinite loop...?

    Well I modified my code and I'm finally getting a number for years. But it's a very big number, about 599296 years.
    This is my modified code...

    public class Lab321
    {
      public static void main(String[] args)
      {
        final double EPSILON = 1.0E-7;
        final double DECAY = 1.14E-4;
        final double INITIAL_AMOUNT = 2.0E-5;
        final double IDEAL = 2.0E-5 / 2;
     
        double amount = INITIAL_AMOUNT;  
        int year = 0;
     
        while (Math.abs(amount - IDEAL) > EPSILON)
        {
          year++;
          double decay = DECAY / 100 * amount;
          amount = amount - decay;
        }  
          System.out.println("The approximate value for the half life of carbon-14 is " + year + " years.");
      }
    }

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: My while loop has run into an infinite loop...?

    without knowing the specifics of how you're calculating the half-life, it's difficult to figure out what's wrong. However, I suspect that the problem is dividing by 100 as your ~100x off.

Similar Threads

  1. Need help with loop
    By ciwas01 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 7th, 2011, 08:50 AM
  2. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  3. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM
  4. Doubling hashing, infinite loop?
    By vluong in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 8th, 2009, 01:26 AM
  5. Do While Loop
    By connex in forum Loops & Control Statements
    Replies: 6
    Last Post: December 7th, 2009, 03:54 PM