Type mismatch: cannot convert from StringBuilder to String

convert string to stringbuilder
convert string to stringbuilder c#
groovy stringbuilder to string
stringbuilder to string array
stringbuilder append
how to convert stringbuilder to int in java
how to convert string to stringbuffer
java lang string cannot be cast to java lang stringbuilder

This method returns the source of the given URL.

private static String getUrlSource(String url) {
    try {
        URL localUrl = null;
        localUrl = new URL(url);
        URLConnection conn = localUrl.openConnection();
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(conn.getInputStream()));
        String line = "";
        String html;
        StringBuilder ma = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            ma.append(line);
        }
        return ma;
    } catch (Exception e) {
        Log.e("ERR",e.getMessage());
    }
}

It gives me this error:

Type mismatch: cannot convert from StringBuilder to String

And two choices:

  1. Change the return type to StringBuilder. But I want it to return a String.
  2. Change type of ma to String. After changing a String has no append() method.

[Solved] Stringbuilder to string conversion in java, Because a StringBuilder is a different class, and cannot be directly used as a String . You must use the toString() method, as described in the  Converts the value of a StringBuilder to a String. Converts the value of this instance to a String. Converts the value of a substring of this instance to a String. Converts the value of this instance to a String. A string whose value is the same as this instance. The following example demonstrates calling the ToString method.

I have same problem while converting StringBuilder to String, and i use above point but that's not give correct solution. using above code output comes like this

    String out=ma.toString();
// out=[Ljava.lang.String;@41e633e0

After that i find out correct solution.Think is create a new String instant inserted of StringBuilder like this..

String out=new String(ma);

Java.lang.StringBuilder.toString() Method, toString() method returns a string representing the data in this sequence. + str); // append integer to the StringBuilder str.append(123); // convert to string object  Type mismatch: cannot convert from java.lang.String to String I tested out some string literal assignments in other classes and it appears to work perfectly fine. I recently updated to Java 1.7.0_15 and recently downloaded and installed Eclipse 4.2.

try

return ma.toString(); as you can not directly store stringbuilder variable into a string variable.

StringBuilder toString() method in Java with Examples , A new String object is created and initialized to get the character sequence from this StringBuilder object and then String is returned by toString(). Subsequent  Type mismatch: cannot convert from StringBuilder to String And two choices: Change the return type to StringBuilder. But I want it to return a String. Change type of ma to String. After changing a String has no append() method.

How to convert String to StringBuilder and vice-versa in Java , StringBuilder to String using toString() method. Let us move forward and discuss above conversions. 1. String to StringBuilder using append()  As a matter of fact, I'm not that familiar with the string type, so a comparison may be done with something like abc.compare( "John" ) Check the documentation for string.

java stringbuilder to string - Search, stackoverflow.com. Type mismatch: cannot convert from StringBuilder to String And two choices: Change the StringBuilder toString() method in Java with  I always get an error: Type mismatch: cannot convert from ChatColor to string And the problematic code is. msg = new StringBuilder (String. valueOf (msg)). append

Java - How to convert StringBuffer to String, The toString() method of StringBuffer class can be used to convert StringBuffer content to a String. This method returns a String object that represents. And: Because many .NET programs use a string parameter type, converting the StringBuilder's buffer to a string is useful. Main: This method creates a StringBuilder and appends 13 strings to its buffer using the Append calls. Finally: We call ToString. This method internally uses logic and often does not copy the string data an additional time.

Comments
  • return ma.toString(); ???
  • As a side note, you might want to ma.append(line).append(LINE_SEPERATOR) or your lines will be all garbled like This is line 1.This is line 2.This is line 3.
  • Function must also return a value in case of exception. If you haven't noticed.
  • @ValeriAtamaniouk Yes they do sometimes, but that's outside the scope of the question. It would make a great comment for the OP, though, like my advice on adding line separators.
  • swallowing exception, logging it and returning null - all 3 antipatterns at the same time. Impressive.
  • Thanks Very Very Much.