Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

306 HOUR 21: Reading and Writing XML Data


26: Element low = day.getFirstChildElement(“low”);
27: Element lowF = low.getFirstChildElement(“fahrenheit”);
28: // get current <icon>
29: Element icon = day.getFirstChildElement(“icon”);
30: // store values in object variables
31: lowTemp[current] = -1;
32: highTemp[current] = -1;
33: try {
34: lowTemp[current] = Integer.parseInt(lowF.getValue());
35: highTemp[current] =
➥Integer.parseInt(highF.getValue());
36: } catch(NumberFormatException nfe) {
37: // do nothing
38: }
39: conditions[current] = icon.getValue();
40: }
41: }
42:
43: public voiddisplay() {
44: for (int i = 0; i < conditions.length; i++) {
45: System.out.println(“Period “+ i);
46: System.out.println(“\tConditions: “+ conditions[i]);
47: System.out.println(“\tHigh: “+ highTemp[i]);
48: System.out.println(“\tLow: “+ lowTemp[i]);
49: }
50: }
51:
52: public static voidmain(String[] arguments) {
53: try{
54: WeatherStation station = new WeatherStation();
55: station.display();
56: } catch(Exception exception) {
57: System.out.println(“Error: “+ exception.getMessage());
58: }
59: }
60: }

The WeatherStationapplication, which requires no command-line argu-
ments, produces seven forecasts for Wasilla, Alaska, as in the following
output:

Output▼
Period 0
Conditions: rain
High: 56
Low: 40
Period 1
Conditions: partlycloudy
High: 59
Low: 40

LISTING 21.3 Continued
Free download pdf