printing a table, please help

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 30
Reputation: shroomiin is an unknown quantity at this point 
Solved Threads: 0
shroomiin shroomiin is offline Offline
Light Poster

printing a table, please help

 
0
  #1
Oct 30th, 2009
hello everyone, i am trying to write a code that displays a table with 75 years. each year has to display the world population and the projected population for the following years after that by multiplying the current population by the yearly growth (1.17) but i am running into some issues. first of all, the current population happens to be 6,816,330,827. but when i try to assign a variable to that it says that it is out of range. whyy is this? also i am not sure how and where to put the values for population. this is what i have so far, just the years column 1-75. where would i go from here?

  1.  
  2. public class WorldPopulation {
  3.  
  4. public static void main(String[] args) {
  5.  
  6.  
  7. int pop ;
  8. double growth=1.17;
  9. {System.out.println("Years " + "Population " + "Increase ");
  10.  
  11. for (int year=1; year<=75; year++)
  12. System.out.println(year);
  13.  
  14.  
  15.  
  16.  
  17. }
  18.  
  19. }
  20. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #2
Oct 30th, 2009
The max value for int is 2147483647, so you will need to use long for that value.
Last edited by Ezzaral; Oct 30th, 2009 at 5:26 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 30
Reputation: shroomiin is an unknown quantity at this point 
Solved Threads: 0
shroomiin shroomiin is offline Offline
Light Poster
 
0
  #3
Oct 30th, 2009
how do i use long??? no matter what i do it keeps giving me errors "token long is an invalid expression"
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #4
Oct 30th, 2009
  1. long pop = 6816330827;
Long is just another primitive data type.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 30
Reputation: shroomiin is an unknown quantity at this point 
Solved Threads: 0
shroomiin shroomiin is offline Offline
Light Poster
 
0
  #5
Oct 30th, 2009
alright thanks! i know i am asking a lot here but i am getting closer. my code and its output are below, but how would i get the population to appear next to the year values, and also get it to repeat all the way down to 75?

  1. public class WorldPopulation {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. long pop=6816330827L;
  6. long population;
  7. double rate=1.17;
  8. {System.out.printf("%s%20s\n", "Year", "Population");
  9.  
  10. for (int year=1; year<=75; year++)
  11.  
  12. System.out.println(year);
  13.  
  14.  
  15. population = (long)(pop*rate);
  16.  
  17. System.out.println(population);
  18.  
  19.  
  20.  
  21. }
  22.  
  23.  
  24. }
  25.  
  26. }

output:

Year Population
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
7975107067



as you can see, its showing up at the bottom.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,514
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #6
Oct 30th, 2009
Concatenate the string you want to print
  1. println(year + " " + population)
or use printf.
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the Java Forum


Views: 165 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC