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

Thread: why is my while loop terminating?

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default why is my while loop terminating?

    Hey everyone so I am having the damndest time trying to find out why when I print out the string from the simulate flight method that it only prints out one line and doesn't go until height reaches 0.
    import java.text.*;
    public class cannonball
    {
       private double v;
       private double t;
       final double G = 9.81;
       private double h;
       private double h1;
       private double maxHeight;
       private String abc;
       private String str;
     
       public cannonball(double Velocity)
        {
            v = Velocity;
            h = 0;
            h1=0; 
            t=0;
     
        }
     
       public String simulateFlight()
        {
          DecimalFormat fmt = new DecimalFormat("#.####");
          t=t+1 ;
          h = (v*t)-(0.5*G*Math.pow(t,2));
          v = v - G*t; 
          h1 = v*t;
          while(h <= 0)
     
          {
                t=t+1;
                h = (v*t)-(0.5*G*Math.pow(t,2));
                v = v - G*t;
                h1 = v*t;
                str =(fmt.format(t)+"\t\t"+fmt.format(v)+"\t\t"+fmt.format(h1)+"\t\t"+fmt.format(h));
          }
          return str;
          }
     
       public double getMaxHeight()
        {
           t=t+0.01; 
           h = (v*t)-0.5*G*Math.pow(t,2);
           h1 = v*t;
     
           while(h <= 0)
     
           {
                t=t+0.01;
                h = (v*t)-0.5*G*Math.pow(t,2);
                h1 = v*t;
                v = v - G*t;
                if(h>h1)
                    maxHeight = h;
                if(h1>h)
                    maxHeight = h1;
     
           }
           return maxHeight;
        }
     
       public String send()
        {
           String abc = "The maximum height is: " + maxHeight;
           return abc;
        }
     
       public String toString()
        {
                    DecimalFormat fmt = new DecimalFormat("#.##");      
            return(fmt.format(t)+"\t\t"+fmt.format(v)+"\t\t"+fmt.format(h1)+"\t\t"+fmt.format(h));
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: why is my while loop terminating?

    Do you have code with a main() method for testing the posted code?
    Also please post the program's output with some comments saying what is wrong with it and show what you want it to be.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: why is my while loop terminating?

    public class cannon 
    {
        public static void main(String[]args)
      {
        cannonball one = new cannonball(100);
     
        System.out.println(one);
        one.simulateFlight();
        System.out.println(one);
        one.simulateFlight();
        System.out.println(one);
         one.simulateFlight();
        System.out.println(one);
         one.simulateFlight();
        System.out.println(one);
     
     
     
     
      }
    }

    Output:
    0 100 0 0
    1 90.19 90.19 95.1
    2 70.57 141.14 180.38
    3 41.14 123.42 255.86
    4 1.9 7.6 321.52

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,482
    My Mood
    Mellow
    Thanks
    151
    Thanked 609 Times in 596 Posts

    Default Re: why is my while loop terminating?

    Please give your variables better names and add a few comments to your code to explain what the heck is supposed to be happening. Single-letter names or the dreaded single-letter plus a number are only appropriate for loop control variables, and even then could usually be improved. There's no penalty for the number of letters used in variable names.

    This is effectively what you're doing in your main() method:
    while ( one.v > 0 )
    {
        one.simulateFlight();
        System.out.println(one);
    }
    Edit: Then it goes into an infinite loop somewhere that I didn't bother to find.

    You can either continue to do that in your main() method, or move that code (modified appropriately) inside the main class structure to achieve the results you desire.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: why is my while loop terminating?

    What is wrong with the 5 lines of the program's output that you posted?

    What is the output supposed to look like? Where is the error in the program's output?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: why is my while loop terminating?

    Should it not print out all the lines when I call the simulateFlight method? Why does it require to be done over and over that's my problem.

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,482
    My Mood
    Mellow
    Thanks
    151
    Thanked 609 Times in 596 Posts

    Default Re: why is my while loop terminating?

    I told you how to do it in post #4. Try that and come back if you need help.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: why is my while loop terminating?

    Can you post what the program currently prints out when it executes
    and also post what the output should look like?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: why is my while loop terminating?

    Difficult to interpret what he is trying to do, but what I take from it is that he wants to call simulateFlight() and then it prints out something like (100, 91, 74, etc...) with one call till it reaches 0. One way that GregBrannon mentioned would be invoking the method through a loop in the main method but I think the OP was trying to get it to run in his simulateFlight() method.

    @frodooftheshire do the math and spot the logic error
       public String simulateFlight()
        {
          DecimalFormat fmt = new DecimalFormat("#.####");
          // These next 4 lines of code are repeated in the loop why have them outside the loop?
          // t = 0 + 1
          // h = (100 * 1) - (0.5 * 9.8 * Math.pow(1, 2));
          // v = 100 - 9.8 * 1;
          // h1 = v * 1;
          // Check if (h <= 0)
          t=t+1 ;
          h = (v*t)-(0.5*G*Math.pow(t,2));
          v = v - G*t; 
          h1 = v*t;
          while(h <= 0)
          {
                t=t+1;
                h = (v*t)-(0.5*G*Math.pow(t,2));
                v = v - G*t;
                h1 = v*t;
                // Why format the string here and then do it again in the toString() method?
                str =(fmt.format(t)+"\t\t"+fmt.format(v)+"\t\t"+fmt.format(h1)+"\t\t"+fmt.format(h));
          }
          // Returns a string but what does the main do with it?  Do you need to return a String at the end of this or does this method handle the printing onto the screen?
          return str;
          }

    In your main the reason it is printing a formatted String is because you call the object System.out.println(one) which then invokes the toString() method of that object. It then gets the current values, formats the string and returns it which is what you see happening in your program.

  10. #10
    Junior Member
    Join Date
    Sep 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: why is my while loop terminating?

    Quote Originally Posted by Ubiquitous View Post
    Difficult to interpret what he is trying to do, but what I take from it is that he wants to call simulateFlight() and then it prints out something like (100, 91, 74, etc...) with one call till it reaches 0. One way that GregBrannon mentioned would be invoking the method through a loop in the main method but I think the OP was trying to get it to run in his simulateFlight() method.

    @frodooftheshire do the math and spot the logic error
       public String simulateFlight()
        {
          DecimalFormat fmt = new DecimalFormat("#.####");
          // These next 4 lines of code are repeated in the loop why have them outside the loop?
          // t = 0 + 1
          // h = (100 * 1) - (0.5 * 9.8 * Math.pow(1, 2));
          // v = 100 - 9.8 * 1;
          // h1 = v * 1;
          // Check if (h <= 0)
          t=t+1 ;
          h = (v*t)-(0.5*G*Math.pow(t,2));
          v = v - G*t; 
          h1 = v*t;
          while(h <= 0)
          {
                t=t+1;
                h = (v*t)-(0.5*G*Math.pow(t,2));
                v = v - G*t;
                h1 = v*t;
                // Why format the string here and then do it again in the toString() method?
                str =(fmt.format(t)+"\t\t"+fmt.format(v)+"\t\t"+fmt.format(h1)+"\t\t"+fmt.format(h));
          }
          // Returns a string but what does the main do with it?  Do you need to return a String at the end of this or does this method handle the printing onto the screen?
          return str;
          }

    In your main the reason it is printing a formatted String is because you call the object System.out.println(one) which then invokes the toString() method of that object. It then gets the current values, formats the string and returns it which is what you see happening in your program.
    Ok your assumption was correct I want to do it in the method itself and what would be my return if it wasn't a formatted string that was my initial problem I didn't know exactly what to return in order for it to display the line after line of output. I'm not sure of how to tell it to print out the results basically until height reaches zero I wasn't sure if there was a logical error in my math but I didn't think there was.

  11. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,482
    My Mood
    Mellow
    Thanks
    151
    Thanked 609 Times in 596 Posts

    Default Re: why is my while loop terminating?

    Something like this will continue to run and print the results while h > 0. I don't think it's exactly what you want, so it's an example of what you could do. You may massage it as needed.
    public String simulateFlight()
    {
        while( h >= 0 )
        {
            System.out.println( this );
     
            t=t+1 ;
            h = (v*t)-(0.5*G*Math.pow(t,2));
            v = v - G*t; 
            h1 = v*t;
     
            v = v - G*t; 
        }
        return str;
    }

Similar Threads

  1. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  2. do while loop terminating to early
    By lupis in forum Loops & Control Statements
    Replies: 3
    Last Post: March 14th, 2012, 02:43 PM
  3. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 07:57 PM
  4. why the while loop is not terminating? plz help me out in this..
    By ab7 in forum Loops & Control Statements
    Replies: 3
    Last Post: December 30th, 2011, 12:48 AM
  5. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM