14 Replies - 427 Views - Last Post: 10 February 2016 - 12:35 PM Rate Topic: -----

#1 Totte  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 18-January 16

Formatting - Newbie Question

Posted 10 February 2016 - 07:36 AM

Hello! I got a question regarding formatting and alignment with printf(). This is my code right now:

System.out.printf("Your time: %s hour(s) %s minute(s) and %s second(s)", hours, minutes, seconds);



It works fine. But I would love to space between "Your time:" and "%s hours %s minutes and %s seconds" without using tab, so it looks something like this:

Your time: | (empty space here) | 1 hour(s) 1 minute(s) and 1 second(s)

Also is there an effective way to automatically write hour or hours depending on the value? I mean some in-built method. Many thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Formatting - Newbie Question

#2 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 10897
  • View blog
  • Posts: 18,590
  • Joined: 19-March 11

Re: Formatting - Newbie Question

Posted 10 February 2016 - 07:42 AM

Something like this, perhaps?
Was This Post Helpful? 1
  • +
  • -

#3 Totte  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 18-January 16

Re: Formatting - Newbie Question

Posted 10 February 2016 - 07:47 AM

View Postjon.kiparsky, on 10 February 2016 - 07:42 AM, said:

Something like this, perhaps?


The variables: hours, minutes and seconds are user inputs with a scanner to inform about their time in a running contest so it is not to get current time of the day. Im gonna read through your link anyway, maybe there is something in there :D Many thanks!!
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6504
  • View blog
  • Posts: 26,300
  • Joined: 12-December 12

Re: Formatting - Newbie Question

Posted 10 February 2016 - 07:59 AM

This link shows how to pad (to justify) strings using printf. Basically, %-15s.

You need an if statement, or ternary operator, to write "hour" or "hours".
Was This Post Helpful? 1
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3504
  • View blog
  • Posts: 15,844
  • Joined: 20-September 08

Re: Formatting - Newbie Question

Posted 10 February 2016 - 08:27 AM

Aren't your variables numerical?? If they are, that should be something like

System.out.printf("Your time: %d hour(s) %02d minute(s) and %02d second(s)%n", hours, minutes, seconds);


Get rid of the last placeholder if you don't want a line break
Was This Post Helpful? 1
  • +
  • -

#6 Totte  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 18-January 16

Re: Formatting - Newbie Question

Posted 10 February 2016 - 08:48 AM

Thanks g00se! It seems both works, but im gonna roll with %d. I managed to make it the time seperate from the text, but it is being depending on how long the text is and thats not really what I want. Let me show you an example with an image :bananaman:

Both of these works ofcourse:
System.out.printf("Your time was: %20d hour(s) %02d minute(s) and %02d second(s)\n", lapInHours, lapInMinutes, lapInSeconds);
System.out.printf("Your time: %20s hour(s) %s minute(s) and %s second(s)", lapInHours, lapInMinutes, lapInSeconds);


And this is the output and an explanation of how I want it:
Posted Image
Was This Post Helpful? 0
  • +
  • -

#7 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 660
  • View blog
  • Posts: 4,983
  • Joined: 25-December 13

Re: Formatting - Newbie Question

Posted 10 February 2016 - 08:53 AM

Quote

Both of these works ofcourse:

A difference is how the fields are adjusted: Strings are to the left and numbers to the right.
Was This Post Helpful? 1
  • +
  • -

#8 Totte  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 18-January 16

Re: Formatting - Newbie Question

Posted 10 February 2016 - 08:55 AM

I mean you could make it like this way, but I want to avoid to add an extra printf :rolleyes2:

System.out.printf("%-20s", "Your time was:");
System.out.printf("%d hour(s) %02d minute(s) and %02d second(s)\n", lapInHours, lapInMinutes, lapInSeconds);
System.out.printf("%-20s", "Your time:");
System.out.printf("%s hour(s) %s minute(s) and %s second(s)", lapInHours, lapInMinutes, lapInSeconds);

Was This Post Helpful? 0
  • +
  • -

#9 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 660
  • View blog
  • Posts: 4,983
  • Joined: 25-December 13

Re: Formatting - Newbie Question

Posted 10 February 2016 - 08:59 AM

Quote

avoid to add an extra printf

All the formatting strings and values could be put into one call to printf().

This post has been edited by NormR: 10 February 2016 - 08:59 AM

Was This Post Helpful? 0
  • +
  • -

#10 Totte  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 18-January 16

Re: Formatting - Newbie Question

Posted 10 February 2016 - 09:15 AM

View PostNormR, on 10 February 2016 - 08:59 AM, said:

Quote

avoid to add an extra printf

All the formatting strings and values could be put into one call to printf().


Could you please give an example? Because I really cant get these two values (times) of these to aligned without being affected by the length of the text:

System.out.printf("Your time: %20d hour(s) %02d minute(s) and %02d second(s)\n", lapInHours, lapInMinutes, lapInSeconds); 
System.out.printf("Your time was: %20d hour(s) %02d minute(s) and %02d second(s)\n", lapInHours, lapInMinutes, lapInSeconds);


The previous image explains what im trying to achieve. Damnit, its tough to be newb :bigsmile: I tried so many different combinations but none works as I want it to, hehe.
Posted Image
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3504
  • View blog
  • Posts: 15,844
  • Joined: 20-September 08

Re: Formatting - Newbie Question

Posted 10 February 2016 - 09:31 AM

Quote

The previous image explains what im trying to achieve.
Which image?
Was This Post Helpful? 0
  • +
  • -

#12 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 660
  • View blog
  • Posts: 4,983
  • Joined: 25-December 13

Re: Formatting - Newbie Question

Posted 10 February 2016 - 09:44 AM

Quote

cant get these two values (times) of these to aligned

Can you type in some text showing what you want? Enclose the text in code tags to preserve the formatting:
    some      text  on two lines
    where     text            the "text" strings are aligned in the same column


Was This Post Helpful? 1
  • +
  • -

#13 Totte  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 69
  • Joined: 18-January 16

Re: Formatting - Newbie Question

Posted 10 February 2016 - 09:53 AM

Oh great! Thanks :bananaman:

Time was:                 1 hour(s) 2 minute(s) 42 second(s)
Your time was:            1 hour(s) 2 minute(s) 42 second(s)
Example time is:          1 hour(s) 2 minute(s) 42 second(s)
Super long time sentence: 1 hour(s) 2 minute(s) 42 second(s)



Theres an example. No matter how long the text is, the time is in a line :)

This post has been edited by Totte: 10 February 2016 - 09:53 AM

Was This Post Helpful? 0
  • +
  • -

#14 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 660
  • View blog
  • Posts: 4,983
  • Joined: 25-December 13

Re: Formatting - Newbie Question

Posted 10 February 2016 - 10:01 AM

Quote

No matter how long the text

Make the width of the first String field be as wide as the longest text to be printed.

Think of the output as going in fixed width columns and create the format strings with the desired width of each column.

This post has been edited by NormR: 10 February 2016 - 10:03 AM

Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3504
  • View blog
  • Posts: 15,844
  • Joined: 20-September 08

Re: Formatting - Newbie Question

Posted 10 February 2016 - 12:35 PM

Then you want something like

System.out.printf("%XXs: %d hour(s) %02d minute(s) and %02d second(s)%n", message, hours, minutes, seconds);


Replace 'XX' with an integer that is at least as large as the number of characters in your longest message, as NormR said
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1