0

I want to put the output of this for loop inside a string and print it at the end. however, it concatenating the old string value with the new one. How can I prevent this issue? help, please!

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);
    }
}
1
  • 1
    where is your variable result declared Commented Mar 13, 2018 at 13:00

3 Answers 3

1

Use a StringBuilder to concatenate value pairs and print them :

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());
Sign up to request clarification or add additional context in comments.

Comments

0

Use a StringBuilder to concatenate all the name-value pairs and print them in the end:

StringBuilder result = new StringBuilder();
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.append(name);
        result.append(" = ");
        result.append(value);
    }
}
System.out.println(result.toString());

You might want to add some separator between the name-value pairs.

1 Comment

i need to print in inside a textarea it printing only the last string
0

You can create a variable outside your for loop and append it with every iteration. After the loop ends you can print your result.

while (tupleQueryResult.hasNext()) {
 BindingSet bindingSet = tupleQueryResult.next();

 String result = ""; // initialize result as empty string

 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 + "\n"; // This will append the row's result to result.
 }
 System.out.println(result);// print after the loop is completed
}

If the number of appends to your result variable are more. i.e., bindingSet.size() is large, prefer using StringBuilder or StringBuffer as these would give faster performance.

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.