944,087 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 334
  • Java RSS
Oct 30th, 2009
0

printing a table, please help

Expand Post »
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?

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
shroomiin is offline Offline
33 posts
since Sep 2009
Oct 30th, 2009
0
Re: printing a table, please help
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Oct 30th, 2009
0
Re: printing a table, please help
how do i use long??? no matter what i do it keeps giving me errors "token long is an invalid expression"
Reputation Points: 10
Solved Threads: 0
Light Poster
shroomiin is offline Offline
33 posts
since Sep 2009
Oct 30th, 2009
0
Re: printing a table, please help
Java Syntax (Toggle Plain Text)
  1. long pop = 6816330827;
Long is just another primitive data type.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Oct 30th, 2009
0
Re: printing a table, please help
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?

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
shroomiin is offline Offline
33 posts
since Sep 2009
Oct 30th, 2009
0
Re: printing a table, please help
Concatenate the string you want to print
Java Syntax (Toggle Plain Text)
  1. println(year + " " + population)
or use printf.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: [JAVA}I am having problem with making Quiz program
Next Thread in Java Forum Timeline: ArrayList null issues...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC