-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw1 problem1
More file actions
79 lines (65 loc) · 2.04 KB
/
Copy pathhw1 problem1
File metadata and controls
79 lines (65 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package hw1;
/* OpenCommercial.java */
import java.net.*;
import java.io.*;
/** A class that provides a main function to read five lines of a commercial
* Web page and print them in reverse order, given the name of a company.
*/
class OpenCommercial {
/** Prompts the user for the name X of a company (a single string), opens
* the Web site corresponding to www.X.com, and prints the first five lines
* of the Web page in reverse order.
* @param arg is not used.
* @exception Exception thrown if there are any problems parsing the
* user's input or opening the connection.
*/
public static void main(String[] arg) throws Exception {
BufferedReader keyboard;
String inputLine;
keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter the name of a company (without spaces): ");
System.out.flush(); /* Make sure the line is printed immediately. */
inputLine = keyboard.readLine();
///my answer:
BufferedReader reader = null;
try {
URL myURL = new URL("http://www."+inputLine+".com/");
reader = new BufferedReader(new InputStreamReader(myURL.openStream()));
}catch (Exception ex) {
ex.printStackTrace();
}
String[] webLine = new String[5];
int i = 0;
if(reader != null) {
while ((i<5) && (webLine[i] = reader.readLine()) != null) {
i++;
}
}
for(i--;i>=0;i--) {
System.out.println(webLine[i]);
}
}
}
// BufferedReader reader = null;
// try {
// URL myURL = new URL("http://www."+inputLine+".com/");
// reader = new BufferedReader(new InputStreamReader(myURL.openStream()));
// }catch (Exception ex) {
// ex.printStackTrace();
//
// }
//
// String[] webLine = new String[5];
// int i = 0;
// if(reader != null) {
// while((i<5) && (webLine[i] = reader.readLine()) != null) {
// i++;
// }
// }
//
// //print the 5 lines in verse oder
// for( i--; i>=0 ;i--) {
// System.out.println(webLine[i]);
// }
// }
//}