Simple Web Input

In Java, reading text from a web page isn't much more difficult than reading from a text file! Download the following code and get it to compile.

Files Needed

import java.net.URL;
import java.util.Scanner;

public class SimpleWebInput
{
    public static void main(String[] args) throws Exception
    {
        URL mcool = new URL("http://cs.leanderisd.org/mitchellis.txt");
        Scanner webIn = new Scanner( mcool.openStream() );

        String one = webIn.nextLine();
        
        webIn.close();
        
        System.out.println(one);
    }
}

What You Should See

Mr. Mitchell is cool.

What You Should Do on Your Own

Assignments turned in without these things will receive half credit or less.

  1. The code provided only reads a single line from the URL given, but mitchellis.txt contains two lines. Add in code to read in and display the second line as well.
  2. Once that works, change the URL to read text from a different web location.


©2013 Graham Mitchell

This assignment is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.
Creative Commons License