Allignment problem

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 461 Views - Last Post: 09 March 2013 - 07:15 PM Rate Topic: -----

#1 glr  Icon User is offline

  • New D.I.C Head

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

Allignment problem

Posted 09 March 2013 - 12:08 AM

public class TableConversion {
public static void main(String[] args) {
// display heading
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?

Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: Allignment problem

#2 Takk  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • 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")
.leftpad("yourstring", length, "paddingcharacter")


Was This Post Helpful? 1
  • +
  • -

#3 glr  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#4 Takk  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 glr  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,875
  • 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.
Was This Post Helpful? 2
  • +
  • -

#7 Takk  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • 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 :)
Was This Post Helpful? 0
  • +
  • -

#8 glr  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • 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*
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,875
  • Joined: 12-December 12

Re: Allignment problem

Posted 09 March 2013 - 03:51 AM

View Postglr, 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.
Was This Post Helpful? 0
  • +
  • -

#10 glr  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#11 glr  Icon User is offline

  • New D.I.C Head

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

Re: Allignment problem

Posted 09 March 2013 - 05:02 AM

Im new to this. I need help!
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,875
  • Joined: 12-December 12

Re: Allignment problem

Posted 09 March 2013 - 05:14 AM

View Postglr, 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.
Was This Post Helpful? 0
  • +
  • -

#13 glr  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#14 glr  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • 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.
Was This Post Helpful? -1
  • +
  • -

#15 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 416
  • View blog
  • 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?
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2