Homework 1-1

来源:互联网 发布:淘宝购买流量怎么退款 编辑:程序博客网 时间:2024/06/16 14:59

Write a program that reads a String from the keyboard. The program should then construct a URL for http://www.X.com/, replacing X with the String read in, and print the first five lines of the Web page at that URL in REVERSE ORDER; i.e., the fifth, fourth, third, second, and first lines.

/* 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. */public 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();          String website = "http://www." + inputLine + ".com/";          //System.out.println(website);          /* Replace this comment with your solution.  */          URL u = new URL(website);          InputStream ins = u.openStream();          InputStreamReader isr = new InputStreamReader(ins);          BufferedReader bfr = new BufferedReader(isr);          String[] s = new String[5];          for(int i = 0; i < 5; i++){              s[i] = bfr.readLine();          }          for(int j = 4; j >= 0; j--){              System.out.println(s[j]);          }      } }

这里写图片描述

(1) readLine

public String readLine() throws IOException

Reads a line of text. A line is considered to be terminated by any one of a line feed (‘\n’), a carriage return (‘\r’), or a carriage return followed immediately by a linefeed.
Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.

Examples:

import java.io.*;public class OpenCommercial{    public static void main(String[] arg) throws Exception {        BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));        String s = keybd.readLine();    }}

InputStream objects (like System.in) read raw data from some source (like the keyboard), but don’t format the data.
InputStreamReader objects compose the raw data into characters (which are typically two bytes long in Java).
BufferedReader objects compose the characters into entire lines of text.

System.out is a PrintStream object that outputs to the screen.
System.in is an InputStream object that reads from the keyboard.

(2) openStream —- read a line of text from the Web page.

public final InputStream openStream() throws IOException

Opens a connection to this URL and returns an InputStream for reading from that connection.

Examples

import java.net.*;import java.io.*;public class OpenCommercial{    public static void main(String[] arg) throws Exception {        String website = "http://www." + s + ".com/";        URL u = new URL(website);        InputStream ins = u.openStream();        InputStreamReader isr = new InputStreamReader(ins);        BufferedReader bfr = new BufferedReader(isr);}
0 0
原创粉丝点击