2

When this code runs, it gets the content of a webpage.

I wanted to concatenate that entire string rather than printing it to the console but when I uncomment the two lines in the code below, System.out.println(inputLine); prints nothing (but it worked with the line below commented) and the value fileText = null,

where does this error come from?

import java.net.*;
import java.io.*;

public class URLReader {

    public static void main(String[] args) throws Exception {

        URL oracle = new URL("http://www.oracle.com");
        BufferedReader in = new BufferedReader(
        new InputStreamReader(oracle.openStream()));

        String fileText = "";
        String inputLine;
        while ((inputLine = in.readLine()) != null)
            //fileText.concat(inputLine);
            System.out.println(inputLine);
        in.close();
        //System.out.println(fileText);
    }
}
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
guillaume guerin
  • 357
  • 2
  • 6
  • 17
  • 2
    Yup, you are right!. The method `concat` hasn't worked since 1996 and none of the 10 million developers who use Java noticed, good catch. :P – Peter Lawrey Aug 10 '12 at 16:23
  • I will edit the question title to be a little more SO friendly – Brian Agnew Aug 10 '12 at 16:26
  • Seems like a duplicate. http://stackoverflow.com/questions/5076740/whats-the-fastest-way-to-concatenate-two-strings-in-java seems to get at the essence of the question (How do I concatenate a java string) even though the linked question is specific to performance. – Aaron Kurtzhals Aug 10 '12 at 16:34

7 Answers7

12

String is immutable and concat() will return a new String (check the linked doc), which you're not collecting.

You should make use of a StringBuilder to build a string efficiently, and then call toString() on that once you're complete to get he resultant String.

e.g.

StringBuilder sb = new StringBuilder();
while (....) {
   sb.append("more string data");
}
String str = sb.toString();

You can append Strings e.g.

   str = str + "more string data";

but it's not very efficient, due to the implementation of String. A StringBuilder is built in order to perform concatenation efficiently. You can tune a StringBuilder via its initial capacity if you have an idea of the size of String you're building.

You may see some sources refer to a StringBuffer. That's very similar, except it's older and synchronises its methods by default. In a non-threaded environment that's wasteful and the general advice is to prefer StringBuilder.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
8

String is immutable object. You need to reassign the concatenated value to a string object. Simply calling fileText.concat(inputLine); will not change the value in fileText.

fileText = fileText.concat(inputLine);

Simply, fileText += inputLine; should work too.

devang
  • 5,376
  • 7
  • 34
  • 49
6

String is immutable, so just calling concat on a String doesn't change it: it returns a new String with the result.

You can instead re-assign the result: fileText = fileText.concat(inputLine);, but since you're in a loop, potentially doing lots of concatenation, it's better to use StringBuilder to do the concatenations, and get a String from the StringBuilder.

pb2q
  • 58,613
  • 19
  • 146
  • 147
4

Hello you must assign the return value of fileText.concat(String) to a variable. In your case to fileText again. The solution for you is:

fileText = fileText.concat(inputLine); 

Have a further look at the Java API -> http://docs.oracle.com/javase/6/docs/api/index.html?java/lang/String.html

zip
  • 579
  • 1
  • 3
  • 16
2

Strings are immutable. This means that fileText.concat(inputLine); does not change the contents of fileText. Rather the concat() method returns a new String with the expected data. You need to capture this return value to get what you want.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
1

change fileText.concat(inputLine) to fileText = fileText.concat(inputLine).

Also, initialize String inputLine = null;

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
1

You should better use StringBuilder for that. Something like

StringBuilder sb = new StringBuilder();
while (...)
    sb.append(line);

It's far more efficient than repeated concatenation of Strings.

Even simpler solution is to use Apache Commons IO library, in particular IOUtils.toString(InputStream,String).

Petr
  • 62,528
  • 13
  • 153
  • 317