September 24 to 30, 2017 was Banned Book Week. People sometimes try to ban books
ID: 3590611 • Letter: S
Question
September 24 to 30, 2017 was Banned Book Week. People sometimes try to ban books because they contain objectionable words, even if the full meaning of the book is very positive. You are to write a program that reads the text of a book on the web and counts the number of times a particular word occurs in the book. Your program should first ask the user for the complete Universal Resource Locator (URL) or link of a file on a website. Typically the user would find it easiest to copy the link at the top of their web browser and paste it as input to your program. Your program should then ask for the word to be searched. The programs output will give the number of times the word appears in the text of the file. You will want to create a java.net.URL object and use that to create a Scanner object to read from the website one line at a time using readLine(). To avoid complications with upper and lower-case characters, your program should convert the user’s input word to lower case and convert all of the input file text to lower case. This will ensure the program finds the words regardless of case.
COMP163 Word Use Count September 24 to 30, 2017 was Banned Book Week. People sometimes try to ban books because they contain objeionable words, even if the full meaning of the book is very positive. You are to wnite a program that reads the text of a book on the web and counts the number of times a particular word occurs in the book. COMP163 Word Use Count Useful String methods Your program should first ask the user for the complete Universal Resource Locator (URL) or link of a file on a website. Typically the user would find it easiest to copy the link at the top of their web browser and paste i as input to your program. Your program should then ask for the word to be searched. The programs output will give the number of times the word appears in the text of the file. public String tolowercase() retums a Stringafter converingall haracters to lower case. You will want to create a java.net.URL object and use that to create a Scanner object to read from the website one line at a time using readtine(0). To avoid complications with upper and lower-case haracters, your program should convert the user's input word to lower case and convert all of the input file text to lower case. This will ensure the program finds the words regardless of case. String mixed "Wixed Case leers: Strin lowermixed.tolowerCaselower get the value "mixed case letters' Remember that a particular word may appear more than once in a line. The indexOf method can be used to search for a word in a String after a specified location. If you find the word in the string, use a public int indexOf(String target) loop to search again after the location the word was found. Retumsthe position in the string of the target or if he trgetis ot inthe sting, The first charscter in Many books that are no longer under copyright are available from www.gutenberz.org For this the string is position zero. assignment, we want to read plain text files, not HTML or PDF files. Some interestingtiles are:example String food - "banana"; Adventures of Huckleberry Finn, by Mark Twain (Samuel Clemens) llpositions 012345 int pos·food. indexof! "na" ); gets the value2; pos food.indexof( "peel"i os gets the value The Tale of Jemima Puddle-Duck, by Beatrix Potter /www.gutenbe /14814/pg14814.xt Romeo and Juliet, by William Shakespeare public int indexOf (String target, int start) Retums the position in the string of the target after location start or-l if the target is no in the string after the specificd star pastion Sample input and output Enter website of book Shttp://www.gutenberg.org/files/76/76-0.txt Enter the word to find >nigger nigger occurs 214 tines String food - "banana"; llpositions 012345 int pos·food.indexofl "na", 3 ); getste value4; os food.indexOE "ba", 31 osh ale Enter website of book >http://www.gutenberg.org/cache/epub/1112/pg1112.txt Enter the word to find >breast breast occurs 6 timesExplanation / Answer
import java.net.*;
import java.io.*;
import java.util.Scanner;
public class cheggBannedBook{
public static void main(String []arg) throws Exception{
Scanner scn = new Scanner(System.in);//for input
System.out.println("Please enter URL");
String urlAddress = scn.next();
URL url = new URL(urlAddress);
System.out.println("Please enter word to be searched");
String word = scn.next();
word = word.toLowerCase();
Scanner urlFile= new Scanner(url.openStream());//to read file content from URL
int count = 0;
while(urlFile.hasNext()){
String line = urlFile.nextLine();
String []words = line.split("\s");//split line on the basis of whitespace in the line
for(String wrd : words){
if((wrd.toLowerCase()).equals(word)){
count++;
}
}
}
System.out.println("Total occurance of "+ word + " is "+ count);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.