For this problem, you will use the sample code given in the
textbook (Uploaded to Canvas) as a
template.
Modify the program so that it reads the webpage
https://openlibrary.org/ and prints book
category title and book category count as a table:
No user input is required. Your challenge is understanding the
structure of the web page
contents and reading the correct data you are interested in.
In Java
sample text
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;
/**
This program prints all lines from a web page that contain
references to other web sites.
*/
public class WebPageReader
{
public static void main(String[] args) throws IOException
{
String address = "http://horstmann.com/index.html";
URL pageLocation = new URL(address);
Scanner in = new Scanner(pageLocation.openStream());
while (in.hasNext())
{
String line = in.next();
if (line.contains("href=\"http://"))
{
int from = line.indexOf("\"");
int to = line.lastIndexOf("\"");
System.out.println(line.substring(from + 1, to));
}
}
}
}
Answer:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** This program prints all lines from a web page that contain references to
* other web sites. */
public class WebPageReader {
public static void main(String[] args) throws IOException {
try {
URL url = new URL("https://openlibrary.org/");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
//formating output into table format
System.out.format("%20s%15s%8s", "Title","|", "Count");
System.out.println("\n-------------------------------------------");
// create pattern for split the data from HTML tag
final Pattern patternForTitle = Pattern.compile("<p class=\"category-title\">(.+?)</p>");
final Pattern patternForCount = Pattern.compile("<p class=\"category-count\" name=\"\">(.+?)</p>");
//iterate all HTML code
while ((inputLine = in.readLine()) != null) {
//check for books title
if(inputLine.contains("category-title")){
final Matcher matcherForTitle = patternForTitle.matcher(inputLine);
matcherForTitle.find();
//print category title
System.out.format("%32s%3s", matcherForTitle.group(1),"|");
}
//check for books count
if(inputLine.contains("category-count")){
final Matcher matcherForCount = patternForCount.matcher(inputLine);
matcherForCount.find();
String count = matcherForCount.group(1).split(" ")[0];
//print books count
System.out.format("%8s", count);
System.out.println();
}
}
in.close();
} catch (MalformedURLException me) {
System.out.println(me);
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}
Output:
Note: I have used https://openlibrary.org/ url for reading the category and books count.
For this problem, you will use the sample code given in the textbook (Uploaded to Canvas)...
ArraysAndFiles.java and PartiallyFilledArray.java. They are shells that do not do anything. You will be adding code after the comments in the main method and using the javadoc documentation to implement the other methods. Task 1: Send array data to the screen In ArraysAndFiles.java, create a new method printOnScreen and compile it. In the main method of ArraysAndFiles.java, add the code to declare and initialize the array of Strings and call printOnScreen to print the array on the screen. Task 2:...
Don't attempt if you can't attempt fully, i will dislike and negative comments would be given Please it's a request. c++ We will read a CSV files of a data dump from the GoodReads 2 web site that contains information about user-rated books (e.g., book titnle, publication year, ISBN number, average reader rating, and cover image URL). The information will be stored and some simple statistics will be calculated. Additionally, for extra credit, the program will create an HTML web...
Don't attempt if you can't attempt fully, i will dislike a nd negative comments would be given Please it's a request. c++ We will read a CSV files of a data dump from the GoodReads 2 web site that contains information about user-rated books (e.g., book tit le, publication year, ISBN number, average reader rating, and cover image URL). The information will be stored and some simple statistics will be calculated. Additionally, for extra credit, the program will create an...
Lab 4: Java Fundamentals, Part IV In this assignment, you solve a conversion problem similar to Programming Challenge 1 of Chapter 3 of your text, page 184 (187 in Edition 5). Given one of the Roman numerals I, II, III, IV, V, VI, VII, VIII, IX, X, XI, XII, XIII, XIV, XV as an input your program must determine and display the corresponding decimal digit 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15....
could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...
*** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J IN CLASS (IF IT DOESNT MATTER PLEASE COMMENT TELLING WHICH PROGRAM YOU USED TO WRITE THE CODE, PREFERRED IS BLUE J!!)*** ArrayList of Objects and Input File Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info...
You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...