1

I was trying to get some HTML by URL and put it into String. This is my effort:

public class 

    Bank {
        public static void main(String[] args) throws IOException {
            URL hh = new URL("https://m.hh.ru/");
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(hh.openStream()));

            String inputLine;
            while ((inputLine = in.readLine()) != null) {

                inputLine.concat(inputLine);//returns null. WTF?
                System.out.println(inputLine);

            }
            in.close();
            System.out.println(inputLine);
        }
        }

As I said I want to put it to inputLine, but it returns NULL. What's wrong?

2
  • 2
    In Java Strings are immutable. Use StringBuilder instead. Commented Aug 24, 2013 at 16:42
  • 1
    There are several problems in this method. Better think it through again. One tip: the last call to in.readLine will set inputLine to null. Commented Aug 24, 2013 at 16:45

1 Answer 1

3

Use a StringBuilder for string concatenation. The loop should look like this:

String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
    sb.append(inputLine).append("\n");
}
System.out.println(sb);
Sign up to request clarification or add additional context in comments.

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.