我遇到的主要问题是从网站解析到我的程序。我得到它来打印出源代码。此外,如果它不包含“http://”,我需要添加它。我真的不明白如何解析字符串。
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class Project6 {
public static void main (String [] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.print("Please enter the URL. ");
String web= sc.nextLine();
String foo = "http://allrecipes.com/";
//is "web" have an allrecipes.com url?
//if it doesn't, then exit
if ( web.equals(foo)) {
StringBuilder s = new StringBuilder();
URL recipes = new URL (web);
BufferedReader in = new BufferedReader(new InputStreamReader(recipes.openStream()));
String inputLine;
while ((inputLine = in.readLine ())!= null)
System.out.println(inputLine);
in.close();
}
else {
System.out.println("I'm sorry, but that is not a valid allrecipes.com URL.");
System.exit(0);
//does "web" start with "http://"
//if it doesn't, add it
}