# why is my while loop terminating?

• October 13th, 2013, 06:42 PM
frodooftheshire
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.
Code :

```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)); } }```
• October 13th, 2013, 06:58 PM
Norm
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.
• October 14th, 2013, 07:40 AM
frodooftheshire
Re: why is my while loop terminating?
Code :

```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
• October 14th, 2013, 09:05 AM
GregBrannon
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:
Code java:

```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.
• October 14th, 2013, 09:43 AM
Norm
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?
• October 14th, 2013, 12:46 PM
frodooftheshire
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.
• October 14th, 2013, 02:03 PM
GregBrannon
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.
• October 14th, 2013, 05:12 PM
Norm
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?
• October 14th, 2013, 08:54 PM
Ubiquitous
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
Code :

``` 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.
• October 15th, 2013, 05:12 PM
frodooftheshire
Re: why is my while loop terminating?
Quote:

Originally Posted by Ubiquitous
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
Code :

``` 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.
• October 15th, 2013, 05:58 PM
GregBrannon
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.
Code java:

```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; }```