Results 1 to 7 of 7
  1. #1
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Need help formatting output

    Hey all here is my code:
    Java Code:
    public static void main(String [] args){
    		
    		System.out.println("Feet \tMeters");
    		
    		for(int i=1;i<=10;i++){
    			double foot =i;
    			System.out.print(" "+foot);
    			System.out.printf("\t  "+"%.2f\n",footToMeter(foot));
    	}
    		
    		System.out.println();
    		System.out.println("Meters \t   Feet");
    		
    		for(int i=20;i<=65;i+=5){
    			double meter =i;
    			System.out.print("  "+meter);
    			System.out.printf("\t   "+"%.2f\n",meterToFoot(meter));
    		}
    		
    	}
    	
    	public static double footToMeter(double foot){
    		
    		double meter = 0.305*foot;
    		return meter;
    		
    	}
    	
    	public static double meterToFoot(double meter){
    		double foot = meter/.305;
    		return foot;
    	}
    Here is the output:
    Java Code:
    Feet 	Meters
     1.0	  0.31
     2.0	  0.61
     3.0	  0.92
     4.0	  1.22
     5.0	  1.53
     6.0	  1.83
     7.0	  2.14
     8.0	  2.44
     9.0	  2.75
     10.0	  3.05
    
    Meters 	   Feet
      20.0	   65.57
      25.0	   81.97
      30.0	   98.36
      35.0	   114.75
      40.0	   131.15
      45.0	   147.54
      50.0	   163.93
      55.0	   180.33
      60.0	   196.72
      65.0	   213.11
    My problem is I want the numbers under Feet to line up so it would look like this (move 10.0 over somehow):
    Java Code:
    Feet 	Meters
     1.0	  0.31
     2.0	  0.61
     3.0	  0.92
     4.0	  1.22
     5.0	  1.53
     6.0	  1.83
     7.0	  2.14
     8.0	  2.44
     9.0	  2.75
    10.0	  3.05
    Also I need it to line up under the second Foot column like this:
    Java Code:
    Meters 	   Feet
      20.0	   65.57
      25.0	   81.97
      30.0	   98.36
      35.0	  114.75
      40.0	  131.15
      45.0	  147.54
      50.0	  163.93
      55.0	  180.33
      60.0	  196.72
      65.0	  213.11
    How would I do this? Thanks for any help and sorry for the long post.

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: Need help formatting output

    Easy way is to check if the number is less than 10, prepend a space. If it is less than 100, prepend another space. Etc.

  3. #3
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Need help formatting output

    Quote Originally Posted by AndrewM16921 View Post
    Easy way is to check if the number is less than 10, prepend a space. If it is less than 100, prepend another space. Etc.
    Okay yeah I see what your saying is there no formatting tool or feature that kicks it over to the left instead of the right built in to java?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,534
    Rep Power
    5

    Default Re: Need help formatting output

    Put a field width modifier after the % sign. In your example, try 6. But read up about how it works in Formatter (Java Platform SE 7 )
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,534
    Rep Power
    5

    Default Re: Need help formatting output

    Not necessary! It can be done with Formatter class.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: Need help formatting output

    java - Formatting numbers with same amount of padding - Stack Overflow

    Can specify a width
    Java Code:
    public static void main(String args[])
    {
    	String format = "%10.2f\n"; // width == 10 and 2 digits after the dot
    	float[] floats = { 123.45f, 99.0f, 23.2f, 45.0f };
    	for(int i = 0; i < floats.length; i++)
    	{
    		float value = floats[i];
    		System.out.format(format, value);
    	}
    }

  7. #7
    codyjava is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default Re: Need help formatting output

    Quote Originally Posted by jim829 View Post
    Put a field width modifier after the % sign. In your example, try 6. But read up about how it works in Formatter (Java Platform SE 7 )
    Okay thanks Jim I ended up using a %4.1f and a %6.2f
    Java Code:
    	public static void main(String [] args){
    		
    		System.out.println("Feet \t  Meters");
    		
    		for(int i=1;i<=10;i++){
    			double foot =i;
    			System.out.printf("%4.1f",foot);
    			System.out.printf("\t  "+"%6.2f\n",footToMeter(foot));
    	}
    		
    		System.out.println();
    		System.out.println("Meters \t    Feet");
    		
    		for(int i=20;i<=65;i+=5){
    			double meter =i;
    			System.out.print("  "+meter);
    			System.out.printf("\t  "+"%6.2f\n",meterToFoot(meter));
    		}
    		
    	}
    	
    	public static double footToMeter(double foot){
    		
    		double meter = 0.305*foot;
    		return meter;
    		
    	}
    	
    	public static double meterToFoot(double meter){
    		double foot = meter/.305;
    		return foot;
    Thanks again

Similar Threads

  1. Formatting Output
    By Rahim2312 in forum New To Java
    Replies: 18
    Last Post: 05-04-2012, 09:45 AM
  2. Help with formatting output
    By John Lord in forum New To Java
    Replies: 1
    Last Post: 10-31-2010, 12:10 PM
  3. formatting numbers in output
    By andy3 in forum New To Java
    Replies: 3
    Last Post: 05-27-2010, 06:43 PM
  4. Need help formatting File Output
    By aaroncarpet in forum New To Java
    Replies: 2
    Last Post: 11-26-2009, 05:26 PM
  5. Need help formatting output and some code
    By A5i19 in forum New To Java
    Replies: 6
    Last Post: 11-09-2009, 04:58 AM

Posting Permissions

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