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

Thread: Please help with simple output.

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

    Default Please help with simple output.

    Hello, This is my first time posting and I'd like to thank you guys in advance and I'm glad I have a resource to help me when I'm completely over my head. I just began a java course and I had to write a program that would display the population each year for five years. I used a linear population increase. But I cannot get the answers to format in an easily recognizable number. I know I could use the int variable, but I'm supposed to be as accurate and it displays as 3.14812582703967E8. Please help and thank you.

    public class Population {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		double births = 60 / 7.0;
    		double birthsPerHour = births * 60;
    		double birthsPerDay = birthsPerHour * 24;
    		double birthsPerYear = birthsPerDay * 365; //births over a year
     
    		double deaths = 60 / 13.0;
    		double deathsPerHour = deaths * 60;
    		double deathsPerDay = deathsPerHour * 24;
    		double deathsPerYear = deathsPerDay * 365;//deaths over a year
     
    		double immigrants = 60 / 45.0;
    		double immigrantsPerHour = immigrants * 60;
    		double immigrantsPerDay = immigrantsPerHour * 24;
    		double immigrantsPerYear = immigrantsPerDay * 365;//immigrants over a year
     
    		int population =  312032486;
    		double population1 =  (population + birthsPerYear + immigrantsPerYear) - deathsPerYear;
     
    	System.out.println("The population for year 1 is " + population1); //print population year 1
     
    		double population2 = (population1 + immigrantsPerYear + birthsPerYear) - deathsPerYear;
     
    	System.out.println("The population for year 2 is " + population2); //print population year 2
     
    		double population3 = (population2 + immigrantsPerYear + birthsPerYear) - deathsPerYear;
     
    	System.out.println("The population for year 3 is " + population3);//print population year 3
     
    		double population4 = (population3 + immigrantsPerYear + birthsPerYear) - deathsPerYear;
     
    	System.out.println("The population for year 4 is " + population4); //print population year 4
     
    		double population5 = population4 + immigrantsPerYear + birthsPerYear - deathsPerYear;
     
    	System.out.println("The population for year 5 is " + population5); //print population year 5
     
     
     
     
    	}
     
    }


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Please help with simple output.

    Welcome to the forum

    Java has two standard methods for formatting output in the manor you want.

    printf() and format()

    You can set how far to the right of the decimal you want the figure to display. Check
    the documentation/tutorials on how these work. Hint: you will need to use the formatter
    which handles floating point numbers.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    thebilljamin (September 9th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Please help with simple output.

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

Similar Threads

  1. Replies: 5
    Last Post: September 7th, 2014, 08:25 AM
  2. [SOLVED] Java runtime get result output from prompt problem with a larger output in type
    By kingwang98 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 14th, 2014, 08:52 AM
  3. Reading characters from a simple output to list words.
    By adwodon in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: July 16th, 2012, 03:34 PM
  4. how could I output to a text area the output of a method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 12th, 2012, 07:49 PM
  5. Simple Input/Output program Acting weird
    By drexasaurus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 19th, 2010, 01:15 PM

Tags for this Thread