views:

33

answers:

2
public void getWebContent() throws Exception {
    Scanner in = new Scanner(System.in);
    System.out.println("Input URL:");
    fileURL = in.nextLine();
    if (!fileURL.startsWith("http://")) {
        System.out.println("URL not valid! Please make sure 'http://' prefixes");
        System.out.println("the web address!");
    } else {
        Scanner url = new Scanner(new URL(fileURL).openStream());
        System.out.println("Here are the contents:");
        while (url.hasNextLine()) {
            System.out.println(url.nextLine());
        }
        System.out.println("Would you like this to be your new Diary?");
        command = in.nextLine();
        if (command.equalsIgnoreCase("yes")) {
            diary.clear();
            while (url.hasNextLine()) {
                diary.add(url.nextLine());
            }
            System.out.println("New Diary created.");
        } else {
            System.out.println("Download cancelled. Returning to first command entry.");
        }
    }
}

So as far as I know, this method getWebContent() works just fine up until it is trying to add the read web file into ArrayList<String> diary. I honestly don't know what's wrong, but I know you smartie-pants out there can figure it out :)!

+1  A: 

By the time you get to that point in the code you have already read all the lines in the Scanner with:

while (url.hasNextLine()) {
    System.out.println(url.nextLine());
}

Scanner has a cursor that advances when you call nextLine()

Romain Hippeau
Thanks, I got it. :)
harris
+1  A: 
    while (url.hasNextLine()) {
        System.out.println(url.nextLine());
    }

This will empty the stream. Nothing will be added to the List because all the data has been read.

As an aside, Scanner is very sensitive to encoding errors - see the ioException() method - to make your code portable, you should use the appropriate encoding for the character data you are reading.

McDowell