Java:字符串:没有给出好的结果
Java: Strings: Not giving a good result
我正在尝试这个教科书示例,我检查了一下,这正是书中的内容:-
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());
}
}
但是它给我的结果是完全错误的。
缓冲区包含
hellonullgoodbyenullabcdefnulltruenullZnull7null10000000000null2.5null33.333nulllast 缓冲区
整件事不应该放在一行中。
这个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);
或者去掉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);
你的问题是你使用 System.getProperty("line.seperator")
而不是 System.getProperty("line.separator")
,你使用 seperator
而不是 separator
打字很简单 我 guess.Elliot 已经回答了其他做事方式,以添加到您的答案中。我更喜欢以下代码:
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);
我更喜欢重用代码,我认为它使代码更健壮。
我正在尝试这个教科书示例,我检查了一下,这正是书中的内容:-
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());
}
}
但是它给我的结果是完全错误的。
缓冲区包含 hellonullgoodbyenullabcdefnulltruenullZnull7null10000000000null2.5null33.333nulllast 缓冲区
整件事不应该放在一行中。
这个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);
或者去掉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);
你的问题是你使用 System.getProperty("line.seperator")
而不是 System.getProperty("line.separator")
,你使用 seperator
而不是 separator
打字很简单 我 guess.Elliot 已经回答了其他做事方式,以添加到您的答案中。我更喜欢以下代码:
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);
我更喜欢重用代码,我认为它使代码更健壮。