Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java I want to read the text from a webpage. The Div to read is the div class and ignore the rest. Cannot get it

Java I want to read the text from a webpage. The Div to read is the div class and ignore the rest. Cannot get it to work gave an example webpage not the ones I will be doing. Needs to read the div, and output top 10 words and times used

import java.io.*; import java.util.*; import org.jsoup.*; import org.jsoup.nodes.*;

public class TextAnalyzer { public static void main(String[] args) throws IOException { Document doc = Jsoup.connect("https://www.oracle.com/corporate/features/jsoup-html-parsing-library.html").get(); Element div = doc.select("div#divClass").first(); String divText = div.text(); Scanner scan = new Scanner(divText); Map map = new HashMap(); while (scan.hasNextLine()){ String val = scan.nextLine(); if(map.containsKey(val) == false) map.put(val,1); else { int count = (int)(map.get(val)); map.remove(val); map.put(val,count+1); // reinserting the word and increase frequncy by 1 } scan.close(); } Set> set = map.entrySet(); List> sortedList = new ArrayList>(set); Collections.sort( sortedList, new Comparator>() { public int compare( Map.Entry a, Map.Entry b ) { return (b.getValue()).compareTo( a.getValue() ); } }); for(Map.Entry i:sortedList){ System.out.println(i.getKey()+" -> "+i.getValue()); } }}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Fundamentals Study Guide

Authors: Dr. Sergio Pisano

1st Edition

B09K1WW84J, 979-8985115307

Students also viewed these Databases questions

Question

What is a separable differential equation? How do you solve it?

Answered: 1 week ago

Question

Create a Fishbone diagram with the problem being coal "mine safety

Answered: 1 week ago

Question

List the common methods used in selecting human resources. page 254

Answered: 1 week ago