# Allignment problem

• (2 Pages)
• 1
• 2

## 20 Replies - 577 Views - Last Post: 09 March 2013 - 07:15 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=314829&amp;s=deb78fa9ff7dbafde018ffcf1dad79c2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 glr

• New D.I.C Head

Reputation: -2
• Posts: 20
• Joined: 20-February 13

# Allignment problem

Posted 09 March 2013 - 12:08 AM

```public class TableConversion {
public static void main(String[] args) {
System.out.println("Miles     Kilometers     Kilometers     Miles");

for (int m= 1, km= 20; m<11; m++, km+=5 ) {
//calculation for kilometer and set decimal place
double kilometer = m * 1.609 * 10;
double totalkilometer = kilometer/10.0;
totalkilometer=(int)(totalkilometer*1000)/1000.0;
// calculation for miles and set decimal place
double miles = km / 1.609 * 10;
double totalmiles = miles/10.0;
totalmiles=(int)(totalmiles*1000)/1000.0;
// Display
System.out.println(m+"          "+totalkilometer+"             "+km+"           "+totalmiles);
}
}
}
```

The above is working but its not looking good. But you can see the screenshot below, starting from line 7-10 the alignment is off. How can I fix that?

Is This A Good Question/Topic? 0

## Replies To: Allignment problem

### #2 Takk

Reputation: 40
• Posts: 111
• Joined: 08-March 13

## Re: Allignment problem

Posted 09 March 2013 - 01:17 AM

Take each columns values and interpret them as strings before you output..

(psuedo like)

```do while miles.length < 3
miles = " " + miles
loop

```

obviously 3 wont be your target length for anything except the first column, after that, decide what your maximum length needs to be and pad to that length, so column 2 would need to pad to atleast 6 character with the current data set, and so forth.

I'm not horribly familiar with java or which packages you have at your disposal, but either the above method, or from a quick search i've turned up the following which you may be able to use. hope this helps.

```org.apache.commons.lang.StringUtils.leftPad("129018", 10, "0")

```

### #3 glr

• New D.I.C Head

Reputation: -2
• Posts: 20
• Joined: 20-February 13

## Re: Allignment problem

Posted 09 March 2013 - 01:23 AM

I haven't learned that yet in college so I don't understand. Any simpler way to fix the alignment?

### #4 Takk

Reputation: 40
• Posts: 111
• Joined: 08-March 13

## Re: Allignment problem

Posted 09 March 2013 - 01:54 AM

There's a trick when you're learning to code.. and generally after you've learned and you ever want to code anything.. read, reread, read some more, then read it again, then play a bit with it, then play some more.. rinse and repeat..

I'm not really familiar with java so don't trust my code verbatim, but a shot in the dark looks like..

Above line 16:
```String sM = org.apache.commons.lang.StringUtils.leftPad(m, 2, " ")

```

Then use the sM variable instead of the m variable on line 16. From the data you've shown above in the screenshot, you'll need to do this to atleast the first two columns.

Keep in mind this is an extremely cumbersome and horribly ugly line of code, a search should return how to import the library (the org.apache.commons.lang part) into your project, and you will be able to call the function by using StringUtils.leftPad() instead of typing all of that every time.

### #5 glr

• New D.I.C Head

Reputation: -2
• Posts: 20
• Joined: 20-February 13

## Re: Allignment problem

Posted 09 March 2013 - 02:11 AM

I don't understand what you said. I want someone to show me a simple fix for this.

### #6 andrewsw

• I'm not here to twist your niblets

Reputation: 4105
• Posts: 14,795
• Joined: 12-December 12

## Re: Allignment problem

Posted 09 March 2013 - 02:21 AM

Here is a straight-forward tutorial on the use of System.out.printf to produce formatted output.

### #7 Takk

Reputation: 40
• Posts: 111
• Joined: 08-March 13

## Re: Allignment problem

Posted 09 March 2013 - 02:54 AM

Nice link andrew, I'll thank you for that one. I commonly forget the point of people doing things my way, then building your way in so they could quit the nonsense

### #8 glr

• New D.I.C Head

Reputation: -2
• Posts: 20
• Joined: 20-February 13

## Re: Allignment problem

Posted 09 March 2013 - 03:32 AM

I've tried the tutorial and replace the Display to this:
```System.out.printf("%10.2f", m, totalkilometer, km, totalmiles);
```

Its not working. I don't know what is wrong as I followed the guide. Can someone the code above.

check*

### #9 andrewsw

• I'm not here to twist your niblets

Reputation: 4105
• Posts: 14,795
• Joined: 12-December 12

## Re: Allignment problem

Posted 09 March 2013 - 03:51 AM

glr, on 09 March 2013 - 03:32 AM, said:

I've tried the tutorial and replace the Display to this:
```System.out.printf("%10.2f", m, totalkilometer, km, totalmiles);
```

Its not working. I don't know what is wrong as I followed the guide. Can someone the code above.

check*

You have one placeholder but you are attempting to supply four values to printf. Study the tutorial again.

### #10 glr

• New D.I.C Head

Reputation: -2
• Posts: 20
• Joined: 20-February 13

## Re: Allignment problem

Posted 09 March 2013 - 04:06 AM

Yeah so whats wrong? I see it can have multiple values.

### #11 glr

• New D.I.C Head

Reputation: -2
• Posts: 20
• Joined: 20-February 13

## Re: Allignment problem

Posted 09 March 2013 - 05:02 AM

Im new to this. I need help!

### #12 andrewsw

• I'm not here to twist your niblets

Reputation: 4105
• Posts: 14,795
• Joined: 12-December 12

## Re: Allignment problem

Posted 09 March 2013 - 05:14 AM

glr, on 09 March 2013 - 05:02 AM, said:

Im new to this. I need help!

The page I linked to is titled "Java For Complete Beginners". If you are unable to follow it then perhaps you should start your studies over from "Day 1" - assuming that you are studying and not just attempting to copy and paste code.

### #13 glr

• New D.I.C Head

Reputation: -2
• Posts: 20
• Joined: 20-February 13

## Re: Allignment problem

Posted 09 March 2013 - 05:41 AM

That is just the basic in the guide. My program has a for loop and I've tried with
```System.out.printf("%10.2f", m, totalkilometer, km, totalmiles);
```

but its not working. So give me a solution how to work with my program.

### #14 glr

• New D.I.C Head

Reputation: -2
• Posts: 20
• Joined: 20-February 13

## Re: Allignment problem

Posted 09 March 2013 - 05:58 AM

Yes I have a trouble following it because its my first time dealing with alignments.

If your ego is too big to help me with this problem that is a piece of cake to you then don't bother. I will wait for someone else who is willing to help me.

### #15 Flukeshot

• A little too OCD

Reputation: 417
• Posts: 1,030
• Joined: 14-November 12

## Re: Allignment problem

Posted 09 March 2013 - 06:05 AM

Does it not help to replace the spaces with tabs?