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

Thread: Need help with printf

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Need help with printf

    Hi, so, I'm having trouble using printf because everytime I try to use it this error comes up
    Enter the year you were born: 
    1993
    Your age is: 21
    You've lived Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '.2f'
    	at java.util.Formatter.format(Unknown Source)
    	at java.io.PrintStream.format(Unknown Source)
    	at java.io.PrintStream.printf(Unknown Source)
    	at Life_Expectancy.main(Life_Expectancy.java:28)

    This is my code and I need to get it to 2 decimal places. Please help!
    import java.util.Calendar;
    import java.util.Scanner;
     
    public class Life_Expectancy {
    	public static void main(String[] args) {
    		//What values - Avg. life expec. = 77.9
    		int year = 0000;
    		int born = 0;
    		int age = 0;
    		double percentage = 0;
    		final double avgLife = 77.9;
     
    		Scanner input = new Scanner(System.in);
    		//User inputs year born
    		System.out.println("Enter the year you were born: ");
    		born = input.nextInt();
     
    		//Get current year (to help calculate age)
    		year = Calendar.getInstance().get(Calendar.YEAR);
    		//Calculate age and percentage lived
    		age = (year - born);
    		// %/100 = age/avgLife 
    		percentage  = ((age / avgLife) * 100);
    		//output (format decimal to 2 places)
    		System.out.println("Your age is: " + age);
    		System.out.printf("You've lived %.2f" +  percentage + "% of your life.");
     
    	}
    }


  2. #2
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Need help with printf

    Hi. The problem is at

    System.out.printf("You've lived %.2f" +  percentage + "% of your life.");

    Here's a quick and handy guide on printf: http://web.cerritos.edu/jwilson/Site..._reference.pdf

    See also Learning Java - Chapter 5 : Tech.

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

    Elyril (March 9th, 2014)

Similar Threads

  1. Formatting using Printf()
    By Dtank123456 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 8th, 2014, 03:48 PM
  2. Help with Printf statement
    By richardman54 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 23rd, 2013, 03:28 PM
  3. Rounding with printf
    By Brandos12 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 24th, 2013, 06:27 PM
  4. Rounding with printf
    By Brandos12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 23rd, 2013, 07:22 PM
  5. Java printf
    By maple1100 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 27th, 2013, 03:20 PM