Results 1 to 3 of 3
  1. #1
    miraclefruit is offline Member
    Join Date
    Nov 2016
    Posts
    10
    Rep Power
    0

    Question Java formatting help

    (Sides of a Triangle) Write an application that reads three nonzero values entered by the user and determines and prints whether they could represent the sides of a triangle.

    Java Code:
    import java.util.Scanner;
    
    public class TriangleYN{
    	
    	public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        double a;
        double b;
        double c;
    
        System.out.print("Enter the size three sizes, decimals acceptable, separated by space:");
        a = input.nextDouble();
        b = input.nextDouble();
        c = input.nextDouble();
    		
    		 if((a+b) > c)
           {
               if((a+c) > b)
               {
                   if((b+c) > a)
                   {
                      System.out.printf("A triangle could measure %.2f%s, %.2f%s, by  %.2f%s.", a, b, c);
                   	  return;
                   }
               }
           }
           System.out.printf("A triangle could not measure %.2f%s, %.2f%s, by  %.2f%s", a, b, c);
       }
    }
    Im having trouble adding decimals to my output. The error is:

    "Given the following was entered from the keyboard:
    4.5 5.5 3.5

    you displayed:
    Enter the size three sizes, decimals acceptable, separated by space:A triangle could measure 4.505.5, 3.50
    instead of:
    Enter the size three sizes, decimals acceptable, separated by space:A triangle could measure 4.50, 5.50, by 3.50."

    Please let me know how to fix this error. Thank you

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Java formatting help

    You don't need the percent s values after each f format specification. They are for strings so get rid of them.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    miraclefruit is offline Member
    Join Date
    Nov 2016
    Posts
    10
    Rep Power
    0

    Default Re: Java formatting help

    Thank you! Works now

    Java Code:
    import java.util.Scanner;
    
    public class TriangleYN{
    	
    	public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        double a;
        double b;
        double c;
    
        System.out.print("Enter the size three sizes, decimals acceptable, separated by space:");
        a = input.nextDouble();
        b = input.nextDouble();
        c = input.nextDouble();
    		
    		 if((a+b) > c)
           {
               if((a+c) > b)
               {
                   if((b+c) > a)
                   {
                      //System.out.printf("A triangle could measure %.2f%f, %.2f%s, by  %.2f%s.", a, b, c);
    				   
    				    System.out.printf("A triangle could measure %.2f, %.2f, by %.2f.", a, b, c);
                   	  return; 
                   }
               }
           }
           System.out.printf("A triangle could not measure %.2f, %.2f, by %.2f.", a, b, c);
       }
    }

Similar Threads

  1. Formatting output string in Java GUI?
    By dojob in forum New To Java
    Replies: 3
    Last Post: 09-17-2013, 07:16 PM
  2. Formatting in Java
    By Deb in forum Advanced Java
    Replies: 5
    Last Post: 02-27-2013, 08:12 PM
  3. java method for number formatting
    By marall80 in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 04-19-2011, 06:25 AM
  4. Formatting Strings in Java
    By ladykrimson in forum New To Java
    Replies: 3
    Last Post: 11-03-2010, 08:31 AM
  5. Formatting Java Code
    By DoubleDee in forum Advanced Java
    Replies: 5
    Last Post: 09-18-2010, 10:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •