1

I'm currently working on a project and with a text based logging system. I give the method my String in the form "Test String \nTest String2 \n Test String3".

This is what the String Formatting looks like.

adminWriter.newLine();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
String currDateTime = dateFormat.format(date);

adminWriter.write(String.format("%-20s | %-50s", currDateTime, text));

And the method is called like this...

Start.scribe.adminToDoWrite("User: "+this.riderID
                                    +"\nTo Delete Horse: "+this.horses[row].getHorseID()
                                    +"\nHorse Name: "+this.horses[row].getHorseName());

The current output looks like this

{Date And Time} | User: (Number)
To Delete Horse: (Number)
Horse Name: (Name)

Is there anyway of getting it to look like this? So that each new line is indented automatically to the String.format rules?

{Date And Time} | User: (Number)
                  To Delete Horse: (Number)
                  Horse Name: (Name)

Many Thanks

4
  • String.format rules? What are those? Commented Jul 25, 2018 at 18:54
  • 1
    "\t" - for Tab Commented Jul 25, 2018 at 18:55
  • 1
    It is \t to put a tab in a string, but it looks like you want the text to shift all the way to the right of the | beside the first variable. If that's the case, you'll need to figure out how long that first string is and add that many spaces to the beginning of the next lines. Commented Jul 25, 2018 at 18:59
  • I recommend you avoid the SimpleDateFormat class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in java.time, the modern Java date and time API. Use for example LocalDateTime.now(yourTimeZone).format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss")). Commented Jul 26, 2018 at 8:54

1 Answer 1

1

You need to change

From:

Start.scribe.adminToDoWrite("User: "+this.riderID
                                +"\nTo Delete Horse: "+this.horses[row].getHorseID()
                                +"\nHorse Name: "+this.horses[row].getHorseName());

To:

Start.scribe.adminToDoWrite("User: "+this.riderID
            +"\n                       To Delete Horse: "+this.horses[row].getHorseID()
            +"\n                       Horse Name: "+this.horses[row].getHorseName());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.