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);}
- Homework 1-1
- Java homework 1
- homework Exercises2.1
- Computer Network Homework 1
- Homework 1/15*2
- linux homework 1
- Homework 1-2
- teacher's homework(1)
- SE homework 1
- Homework Exercise4.1 E1 E2
- 1st homework of OS
- Homework #1 for MAFS 5220
- Homework#1: Review of SmarCity Project
- [Homework#1]回味·神笔马良
- Facebook Hacker Cup 2015 Round 1 --- Homework
- (15 C++ Homework) Alipay System 1 user
- Homework
- homework
- hibernate query.list查询速度慢
- MTK lk源码解析2( lk 阶段main.c中的kmain.c函数解析)
- Tomcat 启动慢
- android用okhttp搭建网络通讯(附服务器端代码)
- PHP 活动人选抽奖示例代码
- Homework 1-1
- CMake学习笔记(一)——CMake官网教程
- Unity AssetBundles-Browser【官方分享】
- 图像小波变换的matlab实现
- bzoj 4403: 序列统计
- Easy 17 Climbing Stairs(70)
- 光立方制作清单
- maven管理jar包的一些问题小结
- 百度编辑器 单独开发图片上传功能