Homework 1-1

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值