0

I made a WebConnection Java script that gets the HTML from a specified website. It works, but when I try to make things easier by automatically concatinating http:// onto the front, it does not work, despite the fact that the Strings should be the same (it gives a java.lang.IllegalArgumentException). Here is my code:

package WebConnection;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JOptionPane;

public class WebConnect {
    URL netPage;

    public WebConnect() {
        String s = "http://";
        s.concat(JOptionPane.showInputDialog(null, "Enter a URL:"));
        System.out.println(s);
        System.out.println(getNetContent(s));

                //The above does not work, but the below does

        //System.out.println(getNetContent(JOptionPane.showInputDialog(null, "Enter a URL:")));
    }

    private String getNetContent(String u) {

        try {
            netPage = new URL(u);
        } catch(MalformedURLException ex) {
            JOptionPane.showMessageDialog(null, "BAD URL!");
            return "BAD URL";
        }
        StringBuilder content = new StringBuilder();
        try{
            HttpURLConnection connection = (HttpURLConnection) netPage.openConnection();
            connection.connect();
            InputStreamReader input = new InputStreamReader(connection.getInputStream());
            BufferedReader buffer = new BufferedReader(input);
            String line;
            while ((line = buffer.readLine()) != null) {
                content.append(line + "\n");
            }
        } catch(IOException e){
            JOptionPane.showMessageDialog(null, "Something went wrong!");
            return "There was a problem.";
        }
        return content.toString();
    }

    public static void main(String[] args) {
        new WebConnect();

    }

For example, if I run first section of webConnect() and type google.com it does not work, but if I run the commented out line instead, and type http://google.com, it does not give an error. Why?

Thanks in advance!

Daniel Causebrook
  • 469
  • 1
  • 8
  • 20

1 Answers1

2

String are immutable. This means you can't edit the contents.

Change...

String s = "http://";
s.concat(JOptionPane.showInputDialog(null, "Enter a URL:"));

To...

String s = "http://";
s = s.concat(JOptionPane.showInputDialog(null, "Enter a URL:"));
David
  • 19,577
  • 28
  • 108
  • 128
  • Even I thought so, but was stumped when I saw this. [MUTABLE STRING](http://stackoverflow.com/questions/11146255/create-a-mutable-java-lang-string). Your answer is correct though:) – Rahul Mar 07 '13 at 16:18
  • Yeah, but I sort of class that the same way private methods can be accessed through reflection, it's not really by the book so to speak. It's not exactly reliable as the contents of the private method could change. – David Mar 07 '13 at 16:21