Вопрос

I am building a validation routine that validates contents and then gives warning (for failures) in form of StringBuilder. Say in below code I am checking lower bound for values paramX and paramY.

 StringBuilder sb= new StringBuilder();

        if(paramX<10){
            sb.append("paramX cannot be less than 10 ");
        }

        if(paramY<20){
            sb.append("paramY cannot be less than 20 ");
        }

        System.out.println(sb);

It gives output as: paramX cannot be less than 10 paramY cannot be less than 20

but i want output such that, each appended String will be printed on new line. Like below.

paramX cannot be less than 10 

paramY cannot be less than 20

I used following workarounds, but ended up repeating same code again and again(Which i don't want to).

sb.append(System.getProperty("line.separator")); // Add Explicit line separator each time
sb.append("\n");
sb.append("paramX cannot be less than 10 \n");

Is there a simpler way to do it?

Это было полезно?

Решение 4

Just thought of sharing the new feature of jdk 8 i used for achieving same result. Using StringJoiner we can construct a sequence of characters separated by a delimiter.

StringJoiner formattedString= new StringJoiner("\n"); 
formattedString.add("XXX");
formattedString.add("YYY");
System.out.println(formattedString);  

Другие советы

If you don't want to do it over and over then write a helper method:

public void appendString(StringBuilder builder, String value) {
    builder.append(value + System.lineSeparator());
}

Then call:

if(paramX<10){
    appendString(sb, "paramX cannot be less than 10 ");
}

This way you only have a single place to maintain if you need to change the output format for the errors.

Another option is to use Apache Commons StrBuilder, which has the functionality that you're looking for.

StrBuilder.appendLn()

The simple way would be to keep a list of errors rather than concatenating them as you go. That would help to maintain a separation of concerns between the logical errors and their presentation.

See how Spring validation works: you have an Errors object that keeps a list of errors, and a separate message source object that fills in the user-visible messages for the different errors.

You could try using a PrintStream as it has an println(String string) method which add the new line automatically.

Something like this.

ByteArrayOutputStream bos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(bos);
ps.println("Line 1");
ps.println("Line 2");
ps.flush();
String message = new String(bos.toByteArray());
System.out.println(message);

First of all you need to include the newline character(\n) at the end of every .append() yourself:

sb.append("paramX cannot be less than 10 \n");

As for repeating you new-line logic just wrap it in a method:

public void append(StringBuilder sb, Object value) {
    sb.append(value).append(System.getProperty("line.separator")).append('\n');
}

And use it like:

if(paramX < 10){
    append(sb, "paramX cannot be less than 10");
}

simply append directly...

        if(paramX<10){
            sb.append("paramX cannot be less than 10 \n ");
        }

        if(paramY<20){
            sb.append("paramY cannot be less than 20 \n ");
        }

Just use \n - it will work everywhere. Also, it looks like you want to conditionally add a line feed if there are two messages:

StringBuilder sb = new StringBuilder();

if (paramX<10) {
    sb.append("paramX cannot be less than 10 ");
}

if (paramY<20) {
    if (!sb.length() > 0) // only add newline if needed
        sb.append('\n');
    sb.append("paramY cannot be less than 20 ");
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top