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

Thread: Printf not working?

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

    Default Printf not working?

    I need to print to 2 decimal places and I'm trying to use the printf method, but for some reason I keep getting an error. I've looked through my code, but unless I'm missing a small detail, it looks okay to me.
    		float sum = 0;
    		float avg = 0;
    		double[] rain = {0, 1.9, 1.5, 1.2, 1.1, 0.5, 0.03, 1.0};
     
    		//Calculate sum of rain.
    		for (int i = 1; i <= 7; i++) {
    			sum += rain[i];
    		}
    		//Calculate average of rain
    		avg = (sum / 7);
     
    		//Printout
    		//Titles
    		System.out.println("Day\t " + " Rain");
    		//rainfall and days
    		for (int j = 1; j <= 7; j++) {
    		System.out.println(j + "\t  " + rain[j]);
    		}
    		//avg rainfall
     
    		System.out.printf("The average rainfall for the week is: %.2f" + avg);
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,439
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Printf not working?

    I keep getting an error
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Printf not working?

    Sorry, meant to put it in...
    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 basicArray.main(basicArray.java:29)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,439
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Printf not working?

    The printf() method takes 2 or more args: The format String and the data to be formatted.
    The statement in the code only has one arg because the + concatenates the number to the first String.
    Replace the + with a ,
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Elyril (March 22nd, 2014)

  6. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Printf not working?

    Quote Originally Posted by Elyril View Post
    Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '.2f'
    See the word "Argument". You are missing an argument. printf/format methods make use of the varargs feature since Java 5.

    printf("format string", arg1, arg2 .............. );

    In your case:

    System.out.printf("The average rainfall for the week is: %.2f", avg);
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  7. The Following 2 Users Say Thank You to andbin For This Useful Post:

    Elyril (March 22nd, 2014), GregBrannon (March 22nd, 2014)

Similar Threads

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