Easy question (print location)

Hi all I have forgotten, how to print chars in a certain location.

For exaple: I have

a=1;

b=12;

c=123;

and I need several columns with certain distance:

column:

1

12

123

[233 byte] By [America70a] at [2007-11-27 11:19:48]
# 1

> Hi all I have forgotten, how to print chars in a

> certain location.

>

Yeah, I hate it when that happens.

So, what have you tried? You got any code?

_helloWorld_a at 2007-7-29 14:38:51 > top of Java-index,Java Essentials,New To Java...
# 2

> Hi all I have forgotten, how to print chars in a

> certain location.

>

> For exaple: I have

>

> > a=1;

> b=12;

> c=123;

>

>

> and I need several columns with certain distance:

>

> column:

> 1

> 12

> 123

I have no idea where would you like to print it.

Are you trying to print at the console then use println(). ^_^

Yannixa at 2007-7-29 14:38:51 > top of Java-index,Java Essentials,New To Java...
# 3

I have something like this:

String name1="Jim Carry"

String name2="Marry Poppins"

String name3="Bill Murray"

String association1="lol";

String association2="goodbye";

String association3="groundhog day"

System.out println(" NAMEASSOCIATION");

//table here

For example, I have string 8, 12,7 characters long and I want them all take 15. So second column starts at 16.

America70a at 2007-7-29 14:38:51 > top of Java-index,Java Essentials,New To Java...
# 4

It's easy enough to make a little left or right padding method for your output. Why not try to create one?

petes1234a at 2007-7-29 14:38:51 > top of Java-index,Java Essentials,New To Java...
# 5

there was a method to do it... or I just mix something

America70a at 2007-7-29 14:38:51 > top of Java-index,Java Essentials,New To Java...
# 6

> there was a method to do it... or I just mix something

hmmmmm, I don't remember one in particular, but I could be wrong. I know that in the past, I've created a little method, just a few lines of code.

petes1234a at 2007-7-29 14:38:51 > top of Java-index,Java Essentials,New To Java...
# 7

Can't printf do something like that?

dwga at 2007-7-29 14:38:51 > top of Java-index,Java Essentials,New To Java...
# 8

Just as you ordered ... here is the code...

apply it and see the results..

public class Test {

public Test() {

}

public static void main(String args[]){

String name1="Jim Carry";

String name2="Marry Poppins";

String name3="Bill Murray";

String association1="lol";

String association2="goodbye";

String association3="groundhog day";

System.out.println("NAMEASSOCIATION");

System.out.println("");

columnFixer(name1,15,association1);

columnFixer(name2,15,association2);

columnFixer(name3,15,association3);

}

public static void columnFixer(String name,int columnWidth,String association){

System.out.print(name);

//the number 4 is for the space that NAME word is taking

int spacesLeft = columnWidth + 4 - name.length();

for (int i = 0; i < spacesLeft; i++) {

System.out.print(" ");

}

System.out.print(association);

System.out.println();

}

}

and the output would be like this:

NAMEASSOCIATION

Jim Carry lol

Marry Poppinsgoodbye

Bill Murraygroundhog day

QussayNajjara at 2007-7-29 14:38:51 > top of Java-index,Java Essentials,New To Java...
# 9

> Can't printf do something like that?

yep, anything that utilizes String.format will work.

petes1234a at 2007-7-29 14:38:51 > top of Java-index,Java Essentials,New To Java...