LAB 38 – Creating and Modifying Text (part 3)

Lab Exercises

Topics

n      To write programs that directly access and use text information from the Internet

n      To access the Internet via a program

n      To use the BlueJ IDE

 

BlueJ (http://www.bluej.org/ [ tutorial at http://www.bluej.org/tutorial/tutorial-201.pdf ]

1.       Starting BlueJ - double-click icon.

2.       Opening a project - select Open from the Project menu and open the people project.

3.       Look at the different classes - right-mouse click and select Edit to view the code

4.       Creating objects - select a constructor from the class popup menu.

5.       Execution - select methods from the object popup menu.

6.       Editing a class - double-click its class icon.

7.       Compilation - To compile a class, click the Compile button in the editor. To compile a project, click the Compile button in the project window.

8.       Help with compiler errors - click the question mark next to the error message.

9.       ENJOY.........

Using BlueJ to start a new program

1.       Starting BlueJ - double-click icon.

2.       Creating a new project - select New Project from the Project menu and save the project as Lab38.

3.       Click on New Class and type TempFinder as the class name

4.       Right-click on the TempFinder class and select Open Editor

5.       Replace the code generated by BlueJ with the code below.

6.       Compile the program and fix any errors that occur.

7.       To run the program, right-click on the TempFinder class and select void main (String[] arguments)

1.       This should pop-up a terminal window with results

12.5 Networks: Getting our text from the Web

i)                    Open a new file in BlueJ as described above and type in the following program:

 

import java.io.*;

import java.net.URL;

/**

 * Class to find the temperature in a web page assuming degree symbol is used

 * taken from textbook with URL changed (ajc now uses flash for weather)

 * modified by Wayne Summers; November 19, 2007

 */

public class TempFinder

{

 

  /**

   * method to get the temperature from a network

   * @param urlStr - the url as a string

   * @return - the temperature as a string

   */

    public static String getTempFromNetwork (String urlStr)

    {

        final String sym = "&#176";  // code for degree symbol (might also appear as &deg)

        String temp = null;

        String line = null;

               

        try        {

            // create a URL

            URL url = new URL(urlStr);

           

            // open a buffered reader on the url

            InputStream inStr = url.openStream();

            BufferedReader reader = new BufferedReader (new InputStreamReader(inStr));

           

            // loop till end of file or find symbol

            while ((line = reader.readLine()) != null && line.indexOf(sym) < 0) {}

           

            // if there is a current line

            if (line != null)

            {

                // find the temperature

                int degreeIndex = line.indexOf(sym);                  //location of degree symbol

                int startIndex = line.lastIndexOf('>', degreeIndex);  //location of tag preceding symbol

                temp = line.substring(startIndex+1, degreeIndex);

            }

        }

        catch (FileNotFoundException ex)    { System.out.println("Couldn't connect to " + urlStr); }

        catch (Exception ex)                          { System.out.println("Error during read or write");

                                                                                    ex.printStackTrace();} 

        return temp;

    }

   

  public static void main(String[] arguments)

  {

      String urlString = "http://weather.ledger-enquirer.com/US/GA/Columbus.html";

      String temp = getTempFromNetwork (urlString);

     

      if (temp == null)

          System.out.println("Sorry, no temp was found at " + urlString);

      else

          System.out.println("The current temp. from " + urlString + " is " + temp + " degrees");

   }

}

QUESTIONS: Nothing to submit in WebCT.