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 3 of 3

Thread: Static Method Help

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Static Method Help

    I'm trying to use a static method to find out how many years it takes to double an arbitrary amount of money so I used a dummy amount of money. The problem is, the code compiles but the code does not run properly. What am I doing wrong?
    class YearsToDouble
    {
       public static void main ( String [] args ) throws Exception
       {
          System.out.println("rate(percent)   years to double");
          for ( int percent : new int[] {1,2,3,4,6,9,12,24} ) line(percent);
       }
       public static void line ( int percent )
       {
          System.out.printf("%7d%17d\n",percent,yearsToDouble(percent));
       }
       public static int yearsToDouble ( int percent )
       {
          double balance = 1.00;
          int years = 0;
          while ( balance < 2.00)
          {
             balance += ((percent/100)*balance);
             years++;
          }
          return years;
       }
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Static Method Help

    while ( balance < 2.00)
          {
             balance += ((percent/100)*balance);
             years++;
          }

    That's an infinite loop.
    1 / 100, 24 / 100, calculated as Integers equate to 0.

    So you're always saying, 0 * balance, which of course equals zero, hence infinite loop
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. The Following User Says Thank You to newbie For This Useful Post:

    mysticCHEEZE (October 9th, 2011)

  4. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    8
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Static Method Help

    Now I get it Thanks!

Similar Threads

  1. "Static method cannot hide instance method from implemented Interface"
    By Gthoma2 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 21st, 2011, 03:03 AM
  2. Accessing static method
    By argolian in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 16th, 2011, 01:44 PM
  3. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 07:51 PM
  4. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  5. Static method
    By kalees in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 20th, 2009, 11:10 AM