Jump to content

printf usage

Go to solution Solved by 79wjd,
7 minutes ago, dirtbikerxz said:

so the way i have my program setup it gives me the position number fore each. like in h = 10. what is the easiest way to generate 9 spaces before that? oh (h position - 1) spaces

A while loop would be the simplest, albeit least elegant form.

 

Something like this, although, I would try and come up with something less cumbersome.

 

Spoiler

int H; T; diff; 

//H and T are set 

if(H > T){
	diff = H-T; 
	for(int i=0; i<T; i++)
		printf(" "); 
	printf("T"); 
	for(int i=0; i<diff; i++)
		printf(" "); 
	printf("H\n"); 
} else {
	diff = T-H; 
	for(int i=0; i<H; i++)
		printf(" "); 
	printf("H"); 
	for(int i=0; i<diff; i++)
		printf(" "); 
	printf("T\n"); 
}

 

 

How do i use System.out.printf() in a way so that a String is displayed exactly x number of spaces from the beginning of the line (not from where a previous string on the same line ended).

 

I am writing a tortoise and hare race program where the output should look like.

 

t h

  t       h

       t                       h

  t                                       h

 

etc.

I already figured out how to find the position of each new move, but I don't know how to display it.

Link to comment
https://linustechtips.com/topic/544878-printf-usage/
Share on other sites

Link to post
Share on other sites

A really simple way would be to just print x number of spaces before printing h/t. 

 

for(int i=0; i<currentMoveSpacing; i++)
	  printf(" "); 

 

 

 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/544878-printf-usage/#findComment-7207083
Share on other sites

Link to post
Share on other sites

3 minutes ago, dirtbikerxz said:

but the problem with that is that its possible for t to be before h, and h to be before t

Are you calculating absolute or relative positioning? If you're doing absolute, then just use an if statement to determine which should be printed first and then print the other x moves after the first where x is the absolute position of the second - the absolute position of the first. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/544878-printf-usage/#findComment-7207114
Share on other sites

Link to post
Share on other sites

absolute. Ya i was trying to avoid doing that, but looks like its my only option. :D

 

so the way i have my program setup it gives me the position number fore each. like in h = 10. what is the easiest way to generate 9 spaces before that? oh (h position - 1) spaces

Link to comment
https://linustechtips.com/topic/544878-printf-usage/#findComment-7207118
Share on other sites

Link to post
Share on other sites

7 minutes ago, dirtbikerxz said:

so the way i have my program setup it gives me the position number fore each. like in h = 10. what is the easiest way to generate 9 spaces before that? oh (h position - 1) spaces

A while loop would be the simplest, albeit least elegant form.

 

Something like this, although, I would try and come up with something less cumbersome.

 

Spoiler

int H; T; diff; 

//H and T are set 

if(H > T){
	diff = H-T; 
	for(int i=0; i<T; i++)
		printf(" "); 
	printf("T"); 
	for(int i=0; i<diff; i++)
		printf(" "); 
	printf("H\n"); 
} else {
	diff = T-H; 
	for(int i=0; i<H; i++)
		printf(" "); 
	printf("H"); 
	for(int i=0; i<diff; i++)
		printf(" "); 
	printf("T\n"); 
}

 

 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/544878-printf-usage/#findComment-7207143
Share on other sites

Link to post
Share on other sites

You can use String.format to add spaces to a string. 

        int h = 1;
        int t = 7;
        int lower = Integer.min(h, t);
        int higher = Integer.max(h, t);
        String formatString = "%1$" + (lower+1) + "s%2$" + (higher - lower) + "s";
        String s = String.format(formatString, (lower == t) ? "t" : "h", (lower == t) ? "h" : "t");

edit: that may have to be changed a bit depending on how you deal with ties.

1474412270.2748842

Link to comment
https://linustechtips.com/topic/544878-printf-usage/#findComment-7207242
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×