Question: **Question after given code** import java.io.*; import java.net.URL; import javax.swing.JOptionPane; /** * Class to find the temperature in a web page. * */ public class

**Question after given code**

import java.io.*; import java.net.URL;

import javax.swing.JOptionPane;

/** * Class to find the temperature in a web page. * */ public class TempFinder { /** * Method to find the temperature in the passed * file * @param fileName the name of the file to look in */ public String getTemp(String fileName) { String seq = "°"; String temp = null; String line = null; // try the following try { // read from the file BufferedReader reader = new BufferedReader(new FileReader(fileName)); // loop till end of file or find sequence while ((line = reader.readLine()) != null && line.indexOf(seq) < 0) {} // if there is a current line if (line != null) { // find the temperature int degreeIndex = line.indexOf(seq); int startIndex = line.lastIndexOf('>',degreeIndex); temp = line.substring(startIndex + 1, degreeIndex); } } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(null,"Couldn't find file " + fileName); } catch (Exception ex) { JOptionPane.showMessageDialog(null,"Error during read or write"); ex.printStackTrace(); } return temp; } /** * Method to get the temperature from a network * @param urlStr the url as a string * @return the temperature as a string */ public String getTempFromNetwork(String urlStr) { String temp = null; String line = null; String seq = "°"; 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 sequence while ((line = reader.readLine()) != null && line.indexOf(seq) < 0) {} // if there is a current line if (line != null) { // find the temperature int degreeIndex = line.indexOf(seq); int startIndex = line.lastIndexOf('>',degreeIndex); temp = line.substring(startIndex + 1, degreeIndex); } } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(null,"Couldn't connect to " + urlStr); } catch (Exception ex) { JOptionPane.showMessageDialog(null,"Error during read or write"); ex.printStackTrace(); } return temp; } public static void main(String[] args) { System.out.print("Reading current temp from stored Atlanta Journal file "); TempFinder finder = new TempFinder(); String temp = finder.getTemp("ajc-weather.html"); if (temp == null) System.out.println("Sorry, no temp was found in the file"); else System.out.println("The current temperature is " + temp); System.out.print(" Reading current temp from Bismarck Tribune "); String urlString = "http://www.bismarcktribune.com/"; temp = finder.getTempFromNetwork(urlString); if (temp == null) System.out.println("Sorry, no temp was found at " + urlString); else System.out.println("The current temp " + "from the network is " + temp); } }

** Explain/describe how the getTempFromNetwork method works; basically describe each line of code **Explain/describe how the 'getTemp' method logic is different from the 'getTempFromNetwork' method logic

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!