Question

For this project, you are tasked with creating a text-based, basic String processing program that performs...

For this project, you are tasked with creating a text-based, basic String processing program that performs basic search on a block of text inputted into your program from an external .txt file. After greeting your end user for a program description, your program should prompt the end user to enter a .txt input filename from which to read the block of text to analyze. Then, prompt the end user for a search String. Next, prompt the end user for the name of the output .txt file to which the user wants the program to print results. Your program should output to a different, external .txt file all of the lines from the input file that contain the user's search String (a match!). Additionally, after your program performs the String analysis and writes the matching lines to the external .txt output file, the program should conclude by printing out to the monitor three statistics:

  • The count/number of matched lines ithat contain the search String (the total number of lines written to the output file);
  • The number of matched lines that start with the search String;
  • The number of matched lines that end with the search String;

Information about the input.txt file:

  • The file should consist of lines of Strings (sentences, phrases, etc.). It should be a block of text, such as a paragraph or a term paper. The lines should not just consist of single Strings containing one word.
  • Each line in the file ends with a hard return (newline -- \n). Don't use word wrap.
  • There can be any number of lines in the input file.
  • Don't worry about whitespace or empty lines; they are legitimate, but obviously won't be a match.
  • You have to be concerned about String case sensitivity -- for instance, if the search String is "Karen," all permutations of upper and lower case letters to form "Karen" must find a match. I'd suggest reviewing the Java API online, as there are String methods that can help you standardize the text prior to searching. There are also specific String methods that can help you perform the searches.
  • The last line in your file should be a String -- not a newline -- \n or blank line.
  • The text in the input file should have data with both upper and lower case letters.

Key requirements:

  • The input text file can be of any length of lines.
  • Do not store the inputted String lines in memory - perform the file output and computations as the data is read into the program, one line (ending with a hard return, or newline), at a time.
  • Utilize repetition and decision structures.
  • Attempt divide and conquer & modularization; create your own static methods in addition to the main() method.
  • Provide a professional user interface (print statements) to the end user.
  • Perform data input validation when interacting with the end user, as appropriate.
  • For the file input, do not use streams; practice the object-based file I/O as discussed in the latter half of chapter 4 (File, Scanner, PrintWriter, FileWriter - as needed).
  • Assume the .txt file(s) are in the same directory as your program.
  • Handle problem conditions. (I am not expecting exception handling at this point.)
  • This should NOT be a GUI program — it should be text-based. (command line / terminal)
  • Be written in a clean format with appropriate comments.

I use these scripts and input files to standardize grading; everyone is graded with the same input via the same process.

This is not required....but.....to make unit testing easier and faster, your program can receive end user manual runtime input via an external file at runtime, instead of having to run your program and manually type commands/input - when you run your program at command prompt. You redirect your program to get manual input from a specific file that contains the necessary manual input, as if typed by an end user manually running your program:

mytest.txt contains the "manual" input your program would need in order to function (the input file name, the search String term, and the output file name):

input.txt
Chernobyl

output.txt

input.txt contains the String block to analyze (the input material).

input.text contains this: Chernobyl suddenly becomes visitor hotspot It was the scene of the world's worst ever nuclear disaster, but three decades later Chernobyl is emerging as one of the planet's hottest tourism tickets after a new TV series. For the past few years, a steady stream of adventurous visitors has been drawn to the macabre spectacle of the deserted, decaying city around the power station. Now, since the recent launch of HBO series "Chernobyl," interest in the location of the horrific nuclear explosion has surged dramatically, according to local tour operators. The mini-series, starring Jared Harris, Stellan Skarsgard and Emily Watson, dramatizes the aftermath of the disaster. It was the highest-ranked TV show on film and TV database IMDB as its fifth and final episode aired earlier this month. Although "Chernobyl" was mainly filmed in Lithuania, its success has seen demand for tours of the area near the infamous Ukrainian site increase by around a third. Located near the city of Pripyat, around 110 kilometers north of Kiev, Chernobyl is one of the most polluted places in the world and can only be visited with a licensed guide. Various tour companies offer guided trips into the "exclusion zone," which covers an area of more than 4,000 square kilometers around the nuclear power plant. "We have seen a 35% rise in bookings," says Victor Korol, director of SoloEast, a tour company which has been offering trips to parts of Chernobyl for two decades. "Most of the people say they decided to book after seeing this show. It's almost as though they watch it and then jump on a plane over." Korol says the most popular tours are one-day group bookings, which cost around $99 per person. Much of the area has been open to tourists since 2011, but some sections, such as the "machine cemetery" of Rossokha village, remain off-limits. However, travelers can visit the abandoned city of Pripyat, as well an observation point around 300 meters away from the New Safe Confinement, a massive steel sarcophagus that covers the remains of the nuclear reactor. According to Korol, the reactor unit and a Ferris Wheel at Pripyat's deserted amusement park are the most popular with visitors. SoloEast has been taking between 100 and 200 visitors to the area on weekend days since "Chernobyl" hit screens. Although Chernobyl has been declared safe to visit, Korol admits that many guests express concerns about radiation levels. "It's the most popular question visitors ask," he says. "But it's absolutely safe. The government would never allow tourists to come otherwise. "The radiation they [visitors] are exposed to on a tour is less than on an intercontinental flight." (If you're wondering how much radiation that is, we're talking less than a chest x-ray). The deadly Chernobyl accident on April 26, 1986, occurred after a routine test at one of the power plant's reactors went catastrophically wrong. While the explosion directly caused around 31 deaths, millions of people were exposed to dangerous radiation levels. Subsequently, the final death toll as a result of long-term radiation exposure is much disputed. Although the UN predicted up to 9,000 related cancer deaths back in 2005, Greenpeace later estimated up to 200,000 fatalities, taking further health problems connected to the disaster into account. Considered the world's worst nuclear power plant accident, the Chernobyl clean-up operation is still in progress 33 years later. https://www.cnn.com/travel/article/chernobyl-tv-tourist-attraction/index.html

mytest.txt contains this:

input.txt
Chernobyl
output.txt

THANK YOU!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner; // Import the Scanner class
import java.io.*;
class Main {
public static void main(String[] args) throws Exception
{
  
Scanner S = new Scanner(System.in); // Scanner object
System.out.println("Enter input file name :: ");
String InputFile = S.nextLine();
System.out.println("Enter string to be searched in file "+InputFile+" :: ");
String searchString = S.nextLine();   
System.out.println("Enter output file name ");
String outputFile = S.nextLine();   
Scanner F = new Scanner(new File(InputFile));
FileWriter fw=new FileWriter(outputFile);
String text;
int match =0, start=0,end=0;
  
   while (F.hasNextLine())
   {
       text=F.nextLine().trim(); // so no space affect
       if(text.toLowerCase().contains(searchString.toLowerCase())) // check if string is in line
       {
       fw.write(text); // write to File
       fw.write("\n");
       match++;
       if(text.toLowerCase().startsWith(searchString.toLowerCase()))
       start++;
       else if(text.toLowerCase().endsWith(searchString.toLowerCase()))
       end++;
       }
   }
fw.close();
System.out.println("Written to "+outputFile+" successfully... ");
System.out.println("The number of matched lines that contain the search String "+searchString+" :: "+match);
System.out.println("The number of matched lines that start with the search String "+searchString+" :: "+start);
System.out.println("The number of matched lines that ends with the search String "+searchString+" :: "+end);
  
}
}
Output file

Chernobyl suddenly becomes visitor hotspot It was the scene of the world's worst ever nuclear disaster, but three decades later Chernobyl is emerging as one of the planet's hottest tourism tickets after a new TV series.
Now, since the recent launch of HBO series "Chernobyl," interest in the location of the horrific nuclear explosion has surged dramatically, according to local tour operators.
Although "Chernobyl" was mainly filmed in Lithuania, its success has seen demand for tours of the area near the infamous Ukrainian site increase by around a third.
Located near the city of Pripyat, around 110 kilometers north of Kiev, Chernobyl is one of the most polluted places in the world and can only be visited with a licensed guide.
"We have seen a 35% rise in bookings," says Victor Korol, director of SoloEast, a tour company which has been offering trips to parts of Chernobyl for two decades.
According to Korol, the reactor unit and a Ferris Wheel at Pripyat's deserted amusement park are the most popular with visitors. SoloEast has been taking between 100 and 200 visitors to the area on weekend days since "Chernobyl" hit screens.
Although Chernobyl has been declared safe to visit, Korol admits that many guests express concerns about radiation levels. "It's the most popular question visitors ask," he says.
The deadly Chernobyl accident on April 26, 1986, occurred after a routine test at one of the power plant's reactors went catastrophically wrong.
Considered the world's worst nuclear power plant accident, the Chernobyl clean-up operation is still in progress 33 years later

Output

Add a comment
Know the answer?
Add Answer to:
For this project, you are tasked with creating a text-based, basic String processing program that performs...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • Write a python program that prompts the user for the names of two text files and...

    Write a python program that prompts the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the scripts should simply output “Yes”. If they are not, the program should output “No”, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a...

  • In this lab, you will write a C program to encrypt a file. The program prompts...

    In this lab, you will write a C program to encrypt a file. The program prompts the user to enter a key (maximum of 5 bytes), then the program uses the key to encrypt the file. Note that the user might enter more than 5 characters, but only the first 5 are used. If a new line before the five characters, the key is padded with 'a'; Note that the new line is not a part of the key, but...

  • 2. Write a function file_copy() that takes two string parameters: in file and out_file and copies...

    2. Write a function file_copy() that takes two string parameters: in file and out_file and copies the contents of in_file into out file. Assume that in_file exists before file_copy is called. For example, the following would be correct input and output. 1 >>> file_copy( created equal.txt', 'copy.txt) 2 >>> open fopen ( copy.txt') 3 >>> equal-f = open( 'created-equal . txt') 4 >>equal_f.read () 5 'We hold these truths to be self-evident, Inthat all men are created equalIn' 3. Write:...

  • Basic C program Problem: In this assignment, you have to read a text file (in.txt) that...

    Basic C program Problem: In this assignment, you have to read a text file (in.txt) that contains a set of point coordinates (x,y). The first line of the file contains the number of points (N) and then each line of the file contains x and y values that are separated by space. The value of x and y are an integer. They can be both negative and positive numbers. You have to sort those points in x-axis major order and...

  • code creating

    Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...

  • Create a program that performs the following operations: Prompt for and accept a string of up...

    Create a program that performs the following operations: Prompt for and accept a string of up to 80 characters from the user. • The memory buffer for this string is created by: buffer: .space 80 # create space for string input • The syscall to place input into the buffer looks like: li $v0, 8 # code for syscall read_string la $a0, buffer # tell syscall where the buffer is li $a1, 80 # tell syscall how big the buffer...

  • Exercise 3) Creating a QT program to search for Customer Phone Number : Create a text...

    Exercise 3) Creating a QT program to search for Customer Phone Number : Create a text file (customer.txt) and put names and phone numbers into it. each name has to have its own phone number right under it. In the QT window, the user has to enter a name and the program prints the phone number related to it. NB: If the user input a name that doesn't exist, the program should print an error message. Text file example: QT...

  • Write a PYTHON program that allows the user to navigate the lines of text in a...

    Write a PYTHON program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT