# Help with java program while loop

• 09-28-2011, 10:43 PM
sfmikejava
Help with java program while loop
Im writing a program that will input the current size of a population, death rate, and birth rate and calculate when the size of the population will hit zero (considering the death rate is higher than the birth rate). I've written my program but im getting an infinite loop and im not sure why? Can someone tell me what im doing wrong?
All help is appreciated.

Here is my program:

import java.util.Scanner;
public class Homework62 {
public static void main(String args[]){
int currentPopulation, numberofIterations;
double birthRate, deathRate, newPopulation;
Scanner stdin = new Scanner(System.in);
System.out.println("Enter the current population size");
currentPopulation = stdin.nextInt();
System.out.println("Enter the birth rate of the population");
birthRate = stdin.nextDouble();
System.out.println("Enter the death rate of the population");
deathRate = stdin.nextDouble();
newPopulation = currentPopulation;
numberofIterations = 0;
System.out.println(newPopulation);
while (newPopulation > 0){
newPopulation = newPopulation - newPopulation*deathRate +newPopulation*birthRate;
numberofIterations++;
}
System.out.println(numberofIterations);
}

}
• 09-28-2011, 10:48 PM
Iron Lion
Re: Help with java program while loop
Try putting a few println() statements inside the loop to see what's going wrong (although you might want to break out of the loop after a while so as not to get flooded with output).
• 09-28-2011, 10:59 PM
sfmikejava
Re: Help with java program while loop
I added some println() statements and im getting some strange output and cant figure out the problem

Output:

Enter the current population size
60000
Enter the birth rate of the population
0.10
Enter the death rate of the population
0.2
1: 54000.0
1: 54000.0
1: 54000.0
1: 54000.0
1: 54000.0
1: 54000.0
1: 54000.0
1: 54000.0
• 09-28-2011, 11:00 PM
sfmikejava
Re: Help with java program while loop
btw the pirntln() i added are:

Code:

```import java.util.Scanner; public class Homework62 {         public static void main(String args[]){                 int currentPopulation, numberofIterations;                 double birthRate, deathRate, newPopulation;                         Scanner stdin = new Scanner(System.in);                         System.out.println("Enter the current population size");                         currentPopulation = stdin.nextInt();                         System.out.println("Enter the birth rate of the population");                         birthRate = stdin.nextDouble();                         System.out.println("Enter the death rate of the population");                         deathRate = stdin.nextDouble();                         newPopulation = currentPopulation;                         numberofIterations = 0;                         System.out.println(newPopulation);                         while (newPopulation > 0){                                 newPopulation = newPopulation - newPopulation*deathRate +newPopulation*birthRate;                                 numberofIterations++;                                 while(numberofIterations < 25){                                         System.out.println(numberofIterations + ": " + newPopulation);                                 }                         }                         System.out.println(numberofIterations);         } }```
• 09-28-2011, 11:03 PM
Fubarable
Re: Help with java program while loop

Original poster, when posting code, place the tag [code] above your code blocks and the tag [/code] below your code block like so:

[code]
[/code]
• 09-28-2011, 11:05 PM
Iron Lion
Re: Help with java program while loop
The reason you get that output is that the inner loop runs separately to the outer loop. As there's nothing in the inner loop to increase numberOfIterations, it will keep going forever.
• 09-28-2011, 11:12 PM
sfmikejava
Re: Help with java program while loop
so i change the while loop to a for and get the output
Enter the current population size
60000
Enter the birth rate of the population
0.1
Enter the death rate of the population
0.2
60000.0
1: 54000.0
2: 48600.0
3: 43740.0
4: 39366.0
5: 35429.4
6: 31886.46
7: 28697.814
8: 25828.0326
9: 23245.229339999998
10: 20920.706405999998
11: 18828.6357654
12: 16945.77218886
13: 15251.194969974
14: 13726.0754729766
15: 12353.467925678939
16: 11118.121133111044
17: 10006.30901979994
18: 9005.678117819947
19: 8105.110306037952
20: 7294.599275434157
21: 6565.139347890741
22: 5908.625413101668
23: 5317.762871791501
24: 4785.986584612351
25: 4307.387926151116
26: 3876.6491335360042
27: 3488.984220182404
28: 3140.085798164164
29: 2826.0772183477475
30: 2543.4694965129725
31: 2289.1225468616753
32: 2060.2102921755077
33: 1854.189262957957
34: 1668.7703366621613
35: 1501.8933029959453
36: 1351.7039726963508
37: 1216.5335754267157
38: 1094.8802178840442
39: 985.3921960956399
40: 886.8529764860759
41: 798.1676788374683
42: 718.3509109537215
43: 646.5158198583493
44: 581.8642378725144
45: 523.677814085263
46: 471.3100326767367
47: 424.17902940906305
48: 381.7611264681567
49: 343.585013821341
50: 309.2265124392069
51: 278.30386119528623
52: 250.47347507575762
53: 225.42612756818187
54: 202.88351481136368
55: 182.5951633302273
56: 164.33564699720458
57: 147.9020822974841
58: 133.1118740677357
59: 119.80068666096213
60: 107.82061799486591
61: 97.03855619537933
62: 87.3347005758414
63: 78.60123051825725
64: 70.74110746643153
65: 63.66699671978837
66: 57.300297047809536
67: 51.57026734302858
68: 46.41324060872573
69: 41.77191654785315
70: 37.59472489306784
71: 33.83525240376106
72: 30.451727163384955
73: 27.40655444704646
74: 24.665899002341813
75: 22.199309102107634
76: 19.97937819189687
77: 17.981440372707183
78: 16.183296335436463
79: 14.564966701892818
80: 13.108470031703536
81: 11.797623028533183
82: 10.617860725679863
83: 9.556074653111878
84: 8.60046718780069
85: 7.740420469020622
86: 6.966378422118559
87: 6.269740579906704
88: 5.642766521916034
89: 5.0784898697244305
90: 4.570640882751988
91: 4.113576794476789
92: 3.70221911502911
93: 3.331997203526199
94: 2.998797483173579
95: 2.6989177348562214
96: 2.4290259613705993
97: 2.1861233652335397
98: 1.9675110287101858
99: 1.770759925839167
100: 1.5936839332552504
101: 1.4343155399297254
102: 1.2908839859367527
103: 1.1617955873430774
104: 1.0456160286087697
105: 0.9410544257478928
106: 0.8469489831731035
107: 0.7622540848557932
108: 0.6860286763702139
109: 0.6174258087331925
110: 0.5556832278598733
111: 0.500114905073886
112: 0.45010341456649744
113: 0.40509307310984766
114: 0.3645837657988629
115: 0.3281253892189766
116: 0.29531285029707893
117: 0.26578156526737107
118: 0.23920340874063398
119: 0.21528306786657056
120: 0.19375476107991352
121: 0.17437928497192218
122: 0.15694135647472995
123: 0.14124722082725696
124: 0.12712249874453127
125: 0.11441024887007814
126: 0.10296922398307032
127: 0.09267230158476328
128: 0.08340507142628695
129: 0.07506456428365825
130: 0.06755810785529243
131: 0.060802297069763186
132: 0.05472206736278687
133: 0.049249860626508185
134: 0.04432487456385737
135: 0.03989238710747163
136: 0.03590314839672447
137: 0.03231283355705202
138: 0.02908155020134682
139: 0.026173395181212138
140: 0.023556055663090922
141: 0.02120045009678183
142: 0.01908040508710365
143: 0.017172364578393284
144: 0.015455128120553954
145: 0.013909615308498558
146: 0.012518653777648703
147: 0.011266788399883834
148: 0.01014010955989545
149: 0.009126098603905905
150: 0.008213488743515315
151: 0.007392139869163783
152: 0.006652925882247405
153: 0.005987633294022664
154: 0.005388869964620398
155: 0.004849982968158358
156: 0.004364984671342523
157: 0.00392848620420827
158: 0.003535637583787443
159: 0.003182073825408699
160: 0.002863866442867829
161: 0.002577479798581046
162: 0.0023197318187229412
163: 0.002087758636850647
164: 0.0018789827731655825
165: 0.0016910844958490244
166: 0.0015219760462641218
167: 0.0013697784416377098
168: 0.0012328005974739389
169: 0.0011095205377265448
170: 9.985684839538902E-4

my code looks like this
Code:

```import java.util.Scanner; public class Homework62 {         public static void main(String args[]){                 int currentPopulation, numberofIterations;                 double birthRate, deathRate, newPopulation;                         Scanner stdin = new Scanner(System.in);                         System.out.println("Enter the current population size");                         currentPopulation = stdin.nextInt();                         System.out.println("Enter the birth rate of the population");                         birthRate = stdin.nextDouble();                         System.out.println("Enter the death rate of the population");                         deathRate = stdin.nextDouble();                         newPopulation = currentPopulation;                         numberofIterations = 0;                         System.out.println(newPopulation);                         while (newPopulation > 0){                                 newPopulation = newPopulation - newPopulation*deathRate +newPopulation*birthRate;                                 numberofIterations++;                                 if(numberofIterations < 600){                                         System.out.println(numberofIterations + ": " + newPopulation);                                 }                         }                         System.out.println(numberofIterations);         } }```
• 09-28-2011, 11:25 PM
Iron Lion
Re: Help with java program while loop
The problem is that it approaches zero but will always be greater than zero. Instead, try testing for >=1, since you can't have 0.98 of a person.

Edit: Better yet, use an int instead of a double for the population, for the same reason as above.