简体   繁体   English

Java:字符串:效果不佳

[英]Java: Strings: Not giving a good result

I am trying this textbook example, and I checked, this is exactly what was in the book:- 我正在尝试这个教科书示例,并检查了一下,这正是书中的内容:-

public class StringBuilderConstructors
{
  public static void main(String[] args)
  {
    Object objectRef = "hello";
    String string = "goodbye";
    char[] charArray = {'a','b','c','d','e','f'};
    boolean booleanValue = true;
    char characterValue = 'Z';
    int integerValue = 7;
    long longValue = 10000000000L;
    float floatValue = 2.5f;
    double doubleValue = 33.333;

    StringBuilder lastBuffer = new StringBuilder("last buffer");
    StringBuilder buffer = new StringBuilder();

    buffer.append(objectRef)
    .append(System.getProperty("line.seperator"))
    .append(string)
    .append(System.getProperty("line.seperator"))
    .append(charArray)
    .append(System.getProperty("line.seperator"))
    .append(booleanValue)
    .append(System.getProperty("line.seperator"))
    .append(characterValue)
    .append(System.getProperty("line.seperator"))
    .append(integerValue)
    .append(System.getProperty("line.seperator"))
    .append(longValue)
    .append(System.getProperty("line.seperator"))
    .append(floatValue)
    .append(System.getProperty("line.seperator"))
    .append(doubleValue)
    .append(System.getProperty("line.seperator"))
    .append(lastBuffer);

    System.out.printf("buffer contains%n%s %n", buffer.toString());
  }
}

But the result it is giving me is completely wrong. 但是它给我的结果是完全错误的。

buffer contains hellonullgoodbyenullabcdefnulltruenullZnull7null10000000000null2.5null33.333nulllast buffer 缓冲区包含hellonullgoodbyenullabcdefnulltruenullZnull7null10000000000null2.5null33.333null最后一个缓冲区

This whole thing is not supposed to be in one line. 整个事情不应该合而为一。

This System.getProperty("line.seperator") is not correct, you wanted System.lineSeparator() like 这个System.getProperty("line.seperator")是不正确的,您想让System.lineSeparator()

buffer.append(objectRef) //
        .append(System.lineSeparator()) //
        .append(string) //
        .append(System.lineSeparator()) //
        .append(charArray) //
        .append(System.lineSeparator()) //
        .append(booleanValue) //
        .append(System.lineSeparator()) //
        .append(characterValue) //
        .append(System.lineSeparator()) //
        .append(integerValue) //
        .append(System.lineSeparator()) //
        .append(longValue) //
        .append(System.lineSeparator()) //
        .append(floatValue) //
        .append(System.lineSeparator()) //
        .append(doubleValue) //
        .append(System.lineSeparator()) //
        .append(lastBuffer);

Or eliminate buffer and use the final printf directly (note printf translates %n appropriately) like 或消除buffer并直接使用最终的printf (注意printf适当地转换%n ),例如

System.out.printf("buffer contains%n%s%n%s%n%s%n%s%n%s%n%s%n%s%n%s%n", 
        objectRef, string, new String(charArray), booleanValue, 
        characterValue, integerValue, longValue, floatValue, doubleValue, 
        lastBuffer);

Your problem is you are using System.getProperty("line.seperator") instead use System.getProperty("line.separator") , you are simple using seperator instead of separator typo i guess.Elliot already answered the other way of doing things, to add to your answer. 您的问题是您使用System.getProperty("line.seperator")而不是System.getProperty("line.separator") ,您很容易使用seperator代替separator我猜。艾略特已经回答了另一种处理方式,以增加您的答案。 I would prefer following code: 我更喜欢以下代码:

String lineSeparator=System.getProperty("line.separator");
buffer.append(objectRef)
.append(lineSeparator)
.append(string)
.append(lineSeparator)
.append(charArray)
.append(lineSeparator)
.append(booleanValue)
.append(lineSeparator)
.append(characterValue)
.append(lineSeparator)
.append(integerValue)
.append(lineSeparator)
.append(longValue)
.append(lineSeparator)
.append(floatValue)
.append(lineSeparator)
.append(doubleValue)
.append(lineSeparator)
.append(lastBuffer);

I prefer reusing the code, i think it make the code more robust. 我更喜欢重用代码,我认为它使代码更健壮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM