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

Thread: Help with formatting output into console

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help with formatting output into console

    Good afternoon! i am working on an assignment an having a bear of a time with one of the requirements. I am to establish formatting of the console output to 2 places after the decimal, but my current code is only showing a single. Any help would be greatly appreciated!!

    import java.util.Scanner;
     
    public class Chpt6_Project {
     
    	public static void main(String[] args) {
    		Scanner input  = new Scanner(System.in);
    		System.out.print("Enter the length: ");
    		double length = input.nextDouble();
    		System.out.print("Enter the width: ");
    		double width = input.nextDouble();
    		System.out.print("Enter the height: ");
    		double height = input.nextDouble();
    		double area = calcArea(length, width, height);
     
    		System.out.printf("The surface area for a rectangular prism "+ length + " inches long, " + width + " "
    				+ "inches wide, and " + height + " inches tall is " + area + " square inches. ");
     
    	}
     
    	public static double calcArea(double num1, double num2, double num3) {
    		double area = ((num1 * num2) + (num1 * num2) + (num1 * num3) + (num1 * num3) + (num2 * num3) + (num2 * num3));
    		return area;
     
    	}
    }

    console is showing:

    Enter the length: 4
    Enter the width: 2
    Enter the height: 1
    The surface area for a rectangular prism 4.0 inches long, 2.0 inches wide, and 1.0 inches tall is 28.0 square inches.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with formatting output into console

    formatting of the console output to 2 places after the decimal,
    Look at the API doc for the java.util.Formatter class to see what format String to use to control the number of decimal places.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Buckeye1 (November 1st, 2019)

  4. #3
    Junior Member
    Join Date
    Oct 2019
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with formatting output into console

    Norm, thank you for the very quick reply. Please excuse the ignorance here, i'm a complete beginner (but loving every minute of learning), but following that guidance it would appear something along the lines of a %4.2f do the trick? establishing a 4 character string, with 2 places after the decimal? i'm getting a compiler error in eclipse when attempting to go that route? not sure if this is a result of bad formatting in the code in requesting the output or if i'm completely off base.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with formatting output into console

    i'm getting a compiler error
    Copy the full text of the error message and paste it here if you have questions about it.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Oct 2019
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with formatting output into console

    i just attempted to format the first 'length' variable here just for testing, but will ultimately need to format length, width, height, and area to two decimals. it let me run the program, but the console spit out the error below

    import java.util.Scanner;
     
    public class Chpt6_Project {
     
    	public static void main(String[] args) {
    		Scanner input  = new Scanner(System.in);
    		System.out.print("Enter the length: ");
    		double length = input.nextDouble();
    		System.out.print("Enter the width: ");
    		double width = input.nextDouble();
    		System.out.print("Enter the height: ");
    		double height = input.nextDouble();
    		double area = calcArea(length, width, height);
     
    		System.out.printf("The surface area for a rectangular prism %4.2d", length + " inches long, " + width + " "
    				+ "inches wide, and " + height + " inches tall is " + area + " square inches. ");
     
    	}
     
    	public static double calcArea(double num1, double num2, double num3) {
    		double area = ((num1 * num2) + (num1 * num2) + (num1 * num3) + (num1 * num3) + (num2 * num3) + (num2 * num3));
    		return area;
     
    	}
    }

    Enter the length: 4
    Enter the width: 2
    Enter the height: 1
    Exception in thread "main" java.util.IllegalFormatPrecisionException: 2
    at java.util.Formatter$FormatSpecifier.checkInteger(U nknown Source)
    at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
    at java.util.Formatter.parse(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.io.PrintStream.format(Unknown Source)
    at java.io.PrintStream.printf(Unknown Source)
    at Chpt6_Project.main(Chpt6_Project.java:16)

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with formatting output into console

    The d is for integer values. Try f for float
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Oct 2019
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help with formatting output into console

    same error


    import java.util.Scanner;
     
    public class Chpt6_Project {
     
    	public static void main(String[] args) {
    		Scanner input  = new Scanner(System.in);
    		System.out.print("Enter the length: ");
    		double length = input.nextDouble();
    		System.out.print("Enter the width: ");
    		double width = input.nextDouble();
    		System.out.print("Enter the height: ");
    		double height = input.nextDouble();
    		double area = calcArea(length, width, height);
     
    		System.out.printf("The surface area for a rectangular prism %4.2f", length + " inches long, " + width + " "
    				+ "inches wide, and " + height + " inches tall is " + area + " square inches. ");
     
    	}
     
    	public static double calcArea(double num1, double num2, double num3) {
    		double area = ((num1 * num2) + (num1 * num2) + (num1 * num3) + (num1 * num3) + (num2 * num3) + (num2 * num3));
    		return area;
     
    	}
    }
    Enter the length: 4
    Enter the width: 2
    Enter the height: 1
    The surface area for a rectangular prism Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String
    at java.util.Formatter$FormatSpecifier.failConversion (Unknown Source)
    at java.util.Formatter$FormatSpecifier.printFloat(Unk nown Source)
    at java.util.Formatter$FormatSpecifier.print(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.io.PrintStream.format(Unknown Source)
    at java.io.PrintStream.printf(Unknown Source)
    at Chpt6_Project.main(Chpt6_Project.java:16)

    --- Update ---

    Thinking about this more... could it be that i haven't established a string? i know i'm not saying this correctly, but i feel like that has something to do with this....???

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help with formatting output into console

    java.util.IllegalFormatConversionException: f != java.lang.String
    There is a different format control character for different data types. So far you have seen d is for int, f is for float, s is for String.
    See the API doc for further types.

    Also read the API doc for the printf method to see what arguments it takes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help with formatting output - PLEASE HELP :(
    By ProgrammablePeter in forum Java Theory & Questions
    Replies: 3
    Last Post: December 2nd, 2013, 10:00 AM
  2. Formatting my output
    By chemical_dog in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 7th, 2013, 07:24 PM
  3. Formatting 2d Array Output
    By dspear in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: December 14th, 2011, 06:45 PM
  4. Formatting Output
    By mael331 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 14th, 2011, 06:41 PM
  5. Replies: 3
    Last Post: August 19th, 2009, 11:30 AM