-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenCommercial.java
More file actions
46 lines (36 loc) · 1.4 KB
/
OpenCommercial.java
File metadata and controls
46 lines (36 loc) · 1.4 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
/* 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();
URL link = new URL("http://www." + inputLine + ".com/");
BufferedReader web = new BufferedReader(new InputStreamReader(link.openStream()));
String strline = web.readLine();
int s = 0;
String[] arrayline = new String[5];
while (s<5 && strline != null) {
arrayline[s] = strline;
s++;
strline = web.readLine();
}
for (int i = 4; i>=0; i--) {
System.out.println(arrayline[i]);
}
}
}