12

I have one method which returns the string but I want the data inside the method in StringBuilder and convert it into string then how can I do it?

  public String[][] getDataOfJD(List<JobDescriptionField> jdFields) {
  int i = 0;
  String [][] data = new String[jdFields.size()][];
  for(JobDescriptionField field : jdFields) {
      if (i > 0){
          String messages = "";
          for (String message : field.getMessages()){
             messages += message;
          }
     data [i] = new String[]{field.getTitle(), field.getField(), messages, field.getScore() + ""};
  }
    i++;
    score = score + field.getScore();
  }
  return data;
  }

From above example field.getTitle(), field.getField() are of String type And if I want the data in StringBuilder and needs to convert it into string but if I tried to do it by return data.toString then it is giving error. Please Help me.

namrata shahade
  • 183
  • 1
  • 1
  • 10
  • 1
    Please share both your attempt and the error message as part of your question. – TimoStaudinger Jun 18 '16 at 19:16
  • Also note that `StringBuffer` and `StringBuilder` are two (similar but) different classes in Java. – TimoStaudinger Jun 18 '16 at 19:17
  • Sorry Actually I want the data variable in StringBuilder not in STringBuffer. And if I convert the data to string then on {field.getTitle(), field.getField(), messages, field.getScore() + ""}; } this line its giving error like incompatibletype . required StringBuilder but it is string – namrata shahade Jun 18 '16 at 19:19
  • What do you want the `StringBuilder` to do? Just concatenate all those strings, without any separators? That'll look really bad. --- *"I tried to do it"* Then show us what you tried, so we can help let you know what you did wrong. – Andreas Jun 18 '16 at 19:38
  • Is the problem solved now? If not what is the format of the output you expect? – ujulu Jun 19 '16 at 07:43

4 Answers4

17

To create a StringBuilder from a String simply:

StringBuilder sb = new StringBuilder("MyString");
String s = sb.toString();

But as was said StringBuilder and StringBuffer are different. Take a look here for more information on StringBuilder and StringBuffer

https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html

Difference between StringBuilder and StringBuffer

Community
  • 1
  • 1
Cary
  • 250
  • 1
  • 7
6

Convert String to StringBuilder

String someText = "something...";
StringBuilder sb = new StringBuilder(someText);
lincollincol
  • 762
  • 2
  • 12
  • 23
1
String str = "rahul";
System.out.println(str.getClass().getName());   
StringBuilder sb1 = new StringBuilder(str); // way 1
StringBuilder sb2 = new StringBuilder(); // way 2
sb2.append(str);
System.out.println(sb1.getClass().getName());
System.out.println(sb2.getClass().getName());

I hope the code above explains everything.

Neuron
  • 5,141
  • 5
  • 38
  • 59
rahul
  • 880
  • 3
  • 14
  • 25
0

StringBuilder has a constructor that accepts string

https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#StringBuilder(java.lang.String)

StringBuilder sb = new StringBuilder("my string");
ifelse.codes
  • 2,289
  • 23
  • 21