Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. I'm trying to right justify my integers but it isn't working

I'm trying to right justify my integers but it isn't working

Scheduled Pinned Locked Moved Java
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 12941702
    wrote on last edited by
    #1

    I have four simple computations in my program. All integer results. I want the output to be right justified but it ain't looking that way. Right now the 5 underneath the 30 looks like it's being placed between the 3 and the 0 (in the above number 30). The -10 looks like it is being correctly right justified. The -10 looks like it is being correctly right justified with the 30. And the 200 takes up the maximum field width of 3 which I have set. My output has a ragged right appearance so I don't know where I'm going wrong. My code looks like this:

    public class FormattingOutput
    {
    public static void main(String args[])
    {

    	int a, b, c, d;
    	
    	a = 10 + 20;
    	b = 10 - 5;
    	c = 10 - 20;
    	d = 10 \* 20;
    	
    	System.out.printf("%3d%n", a);
    	System.out.printf("%3d%n", b);
    	System.out.printf("%3d%n", c);
    	System.out.printf("%3d", d);
    }
    

    }

    U M 2 Replies Last reply
    0
    • U User 12941702

      I have four simple computations in my program. All integer results. I want the output to be right justified but it ain't looking that way. Right now the 5 underneath the 30 looks like it's being placed between the 3 and the 0 (in the above number 30). The -10 looks like it is being correctly right justified. The -10 looks like it is being correctly right justified with the 30. And the 200 takes up the maximum field width of 3 which I have set. My output has a ragged right appearance so I don't know where I'm going wrong. My code looks like this:

      public class FormattingOutput
      {
      public static void main(String args[])
      {

      	int a, b, c, d;
      	
      	a = 10 + 20;
      	b = 10 - 5;
      	c = 10 - 20;
      	d = 10 \* 20;
      	
      	System.out.printf("%3d%n", a);
      	System.out.printf("%3d%n", b);
      	System.out.printf("%3d%n", c);
      	System.out.printf("%3d", d);
      }
      

      }

      U Offline
      U Offline
      User 12941702
      wrote on last edited by
      #2

      I know now. You have to use a fixed width font in your console. I wasn't.

      1 Reply Last reply
      0
      • U User 12941702

        I have four simple computations in my program. All integer results. I want the output to be right justified but it ain't looking that way. Right now the 5 underneath the 30 looks like it's being placed between the 3 and the 0 (in the above number 30). The -10 looks like it is being correctly right justified. The -10 looks like it is being correctly right justified with the 30. And the 200 takes up the maximum field width of 3 which I have set. My output has a ragged right appearance so I don't know where I'm going wrong. My code looks like this:

        public class FormattingOutput
        {
        public static void main(String args[])
        {

        	int a, b, c, d;
        	
        	a = 10 + 20;
        	b = 10 - 5;
        	c = 10 - 20;
        	d = 10 \* 20;
        	
        	System.out.printf("%3d%n", a);
        	System.out.printf("%3d%n", b);
        	System.out.printf("%3d%n", c);
        	System.out.printf("%3d", d);
        }
        

        }

        M Offline
        M Offline
        Member 13615896
        wrote on last edited by
        #3

        Your output sentence is wrong.This is JAVA ,is not C.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups