我想将此for循环的输出放在字符串中,并在最后打印。但是,它将旧的字符串值与新的字符串值连接在一起。如何预防此问题?
请帮助!

while (tupleQueryResult.hasNext()) {
    BindingSet bindingSet = tupleQueryResult.next();
    for (Binding binding : bindingSet) {
        // Each Binding contains the variable name and the value for this result row
        String name = binding.getName();
        Value value = binding.getValue();
        result = name + " = " + value;
        System.out.println(result);
    }
}

最佳答案

使用StringBuilder连接值对并打印它们:

StringBuilder string= new StringBuilder();
String name;
Value value;
while (tupleQueryResult.hasNext())
{

    BindingSet bindingSet = tupleQueryResult.next();

    for (Binding binding : bindingSet)
     {
       name = binding.getName();
       value = binding.getValue();
       string.append(name);
       string.append(" = ");
       string.append(value);
       string.append("\n");   // to enter newline character
     } //for

 }  //while

System.out.println(string.toString());

关于java - 将循环结果放入Java中的字符串中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49256703/

10-13 01:39