#How to generate a line using the length of the longest text

9 messages · Page 1 of 1 (latest)

whole stratus
#

Hello, so i am trying to create a line of dashes to separate different sections in the output of and i wanted to now if there is a way to repeated print ("-"); or something similar using the length on the longest line of text.

For Example

System.out.println ("Placeholder for Variables");  // this is shorter so it would not count the length of this text
System.out.println ("Are your trying to purchase a Ticket?"); // this is longer so it would count the length of this 
System.out.println ("-------------------------------------"); // creates this line without having to be manually input like this to match the length of the text above 

I know I could just manually make these lines but I plan on have statements with variables that change in length depending on user input so i want to make it generate these lines on its own.
Do not know if this would work since i am completely new to java, but i was thinking of making the lines of code where i concat a user input variable a string and get the length of that, but then again i have no idea how to do that and i still don't know how to make it create lines by itself.

If i provided to little info and you need something else just ask

wise flowerBOT
#

This post has been reserved for your question.

Hey @whole stratus! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

whole stratus
#

Also, another question. how do you get the length of text inside a println statement

buoyant current
#

.length()?

#

.repeat(int) also exists

whole stratus
#

sorry, im very new to java, how would i use the .length() to count the words in the bracket of the print statement and the .repeat(int) to repeat "-" (length) amount of times

#

nevermind i figured it out, thanks

wise flowerBOT
# whole stratus nevermind i figured it out, thanks

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.