Question

In: Computer Science

Question: Can I get the code in Java for this assignment to compare? Please and thank you....

Question: Can I get the code in Java for this assignment to compare? Please and thank you.

Can I get the code in Java for this assignment to compare? Please and thank you.
Description
Write a Java program to read data from a text file (file name given on command line), process the text file by performing the following:

Print the total number of words in the file.


Print the total number of unique words (case sensitive) in the file.


Print all words in ascending order without duplication.


Print all line(s) with line number(s) of the file where a specific phrase is found. You must prompt the user to enter a specific phrase to search for. Under each output line indicate the location(s) of the first character of the matched phrase (refer to the Sample Ouput below).


After outputting results keep prompting user until the user enters EINPUT.


You must enter a multi-line comment before EACH method declaration. In the comment you must provide a small description of this method. Follow the template below:
/**
* <write your description of method here>
* @param <parameter name here> <write description of parameter is here>
* @return <write description of what is returned here>
*/
You must handle all Exceptions appropriately:
Must output an appropriate message.
Must put the program into a state in which the user can recover from or exit if necessary.
There must be at a minimum the following methods:
Name: main
Description: The main method should do the following.
Retrieve the file name from the command line.
The program should exit if the file does not exist or if the file name is not given on the command line. When exiting the program should print the message “File does not exist.”
If the file exists the main method should call the processFile method. Any data that is needed from the main method must be passed to the processFile method via its parameters. See processFile method below.
Name: processFile
Description: The processFile method should read the contents of the file. The processFile method should print out the following:
Print the total number of words in the file.
Print the total number of unique words (case sensitive) in the file.
Print all words in ascending order without duplication.
Once the information above is printed to the console the processFile method should call the search method. Any data that is needed from the processFile method must be passed to the search method via its parameters.
Name: search
Description: The search method should prompt the user for a search phrase. Given the search phrase the search method should search each line in the file for the location of the phrase on each line. If the search phrase is not found on a line then just go to the next line. The search method must find every occurrence of the phrase on a line. The search method should print the line number, the line and the location of the search phrase (“use ^”). After all lines have been searched the search method then prompts the user to enter another search phrase. The search method does not exit until the user enters the phrase EINPUT.

random.txt file
Her old collecting she considered discovered.
So at parties he warrant oh staying. Square new horses and put better end.
Sincerity collected happiness do is contented.
Sigh ever way now many. Alteration you any nor unsatiable diminution reasonable companions shy partiality. Leaf by left deal mile oh if easy.
Added woman first get led joy not early jokes.
Are own design entire former get should.
Advantages boisterous day excellence boy. Out between our two waiting wishing.
Pursuit he he garrets greater towards amiable so placing.
Nothing off how norland delight. Abode shy shade she hours forth its use.
Up whole of fancy ye quiet do. Justice fortune no to is if winding morning forming.


Solutions

Expert Solution

Code


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;


public class Main
{
public static void main(String[] args)
{
BufferedReader reader;
String fileName=args[0];
try
{
reader = new BufferedReader(new FileReader(fileName));
processFile(reader);
}
catch (IOException e)
{
System.out.println("Sorry! File not found...");
}
}
/**
* <method reads the line from the input file and stores into the array list of string than count the
* count the wprds and then remove dublicates and add the unique words into the another arraylist of string.
* then prints the result>
* @param <object of the BufferedReader> <this object is used to read the file line by line>
* this method returns nothing
*/
private static void processFile(BufferedReader reader) throws IOException
{
ArrayList<String>lines=new ArrayList<>();
ArrayList<String>uniqWords=new ArrayList<>();
int wordCount=0;
String str = reader.readLine();
while (str != null)
{
       lines.add(str);
String word[]=str.split(" ");
wordCount+=word.length;
for(int i=0;i<word.length;i++)
{
  
String w=word[i].replaceAll("\\p{Punct}","");
if(!uniqWords.contains(w))
uniqWords.add(w);
}
str = reader.readLine();
}
reader.close();
System.out.println("Total number of words in file: "+wordCount);
System.out.println("Total number of unique words in file: "+uniqWords.size());
System.out.println("Unique words of the input file in ascending order:");
Collections.sort(uniqWords);
  
for(int i=0;i<uniqWords.size();i++)
{
System.out.println(uniqWords.get(i));
}
search(lines);
}
/**
* <ask user to enter the pharse and then search the pharse in every line if found then prints the line number and line.>
* @param <array list of strings> <write description of parameter is here>
* @return <nothing>
*/
private static void search(ArrayList<String> lines)
{
System.out.println("");
Scanner sc=new Scanner(System.in);
while(true)
{   
System.out.print("Enter Search Pattern: ");
String pharse=sc.nextLine();
if(pharse.equalsIgnoreCase("EINPUT"))
break;
for(int i=0;i<lines.size();i++)
{
if(lines.get(i).contains(pharse))
{
int pos=lines.get(i).indexOf(pharse);
System.out.println("Line Number "+(i+1));
System.out.println(lines.get(i));
for(int j=0;j<pos-1;j++)
System.out.print("_");
System.out.println("^");
}
}
}
}
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Please I can get a flowchart and a pseudocode for this java code. Thank you //import...
Please I can get a flowchart and a pseudocode for this java code. Thank you //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import...
Please can I get a flowchart and pseudocode for this java code. Thank you. TestScore.java import java.util.Scanner; ;//import Scanner to take input from user public class TestScore {    @SuppressWarnings("resource")    public static void main(String[] args) throws ScoreException {//main method may throw Score exception        int [] arr = new int [5]; //creating an integer array for student id        arr[0] = 20025; //assigning id for each student        arr[1] = 20026;        arr[2] = 20027;...
Please can I kindly get a flowchart for this java code. Thank you. //import the required...
Please can I kindly get a flowchart for this java code. Thank you. //import the required classes import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BirthdayReminder {       public static void main(String[] args) throws IOException {        // declare the required variables String sName = null; String names[] = new String[10]; String birthDates[] = new String[10]; int count = 0; boolean flag = false; // to read values from the console BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in));...
please right make it so that it can run on jGRASP and java code please thank...
please right make it so that it can run on jGRASP and java code please thank you Debug Problem 1: As an intern for NASA, you have been instructed to debug a java program that calculates the speed that sound travels in water. Details about the formulas and correct results appear in the comments area at the top of the program Here is the code to debug: importjava.util.Scanner; /**    This program demonstrates a solution to the    The Speed of Sound...
this is financial Accounting. please provide the working details i can get it. thank you. Vulture...
this is financial Accounting. please provide the working details i can get it. thank you. Vulture Demolition Ltd has five employees and needs to calculate its Long Service Leave (LSL) expense for the year ended 30 June 2019. Employees are entitled to 15 weeks LSL after 12 years of service. If employees leave the business after 10 years but before 12 years they are entitled to be paid out their accumulated LSL. Employees who leave prior to 10 years service...
HELLO CAN YOU PLEASE DO THIS JAVA PROGRAM I WILL LEAVE AWESOME RATING. THANK YOU IN...
HELLO CAN YOU PLEASE DO THIS JAVA PROGRAM I WILL LEAVE AWESOME RATING. THANK YOU IN ADVANCE. QUESTION Suppose you are designing a game called King of the Stacks. The rules of the game are as follows:  The game is played with two (2) players.  There are three (3) different Stacks in the game.  Each turn, a player pushes a disk on top of exactly one of the three Stacks. Players alternate turns throughout the game. Each...
In Java please. Thank you! Recursion For this assignment you are going to write six different...
In Java please. Thank you! Recursion For this assignment you are going to write six different methods. Each method is to be written recursively. Any method that is written iteratively will not receive any credit, even if it is correct and produces the same results or output. You will be given a starter file. You are not allowed to change the signatures of any of the given methods. You are not allowed to add any methods to your solutions. Write...
C# Tip Calculator. I can't figure the code out for this assignment can I get some...
C# Tip Calculator. I can't figure the code out for this assignment can I get some help For this assignment, you'll create a simple tip calculator. Expected program flow: Ask the user for the bill total. Ask the user for the tip percent. Print the final output in the following format when the user enters 10 and 15 for the first two prompts: Total for bill $10.00 with a 15% tip is $11.50 Note that the money values should be...
It's Java; the code should be the same as the output sample below; please, thank you....
It's Java; the code should be the same as the output sample below; please, thank you. Note: create a single-file solution that has multiple class definitions. Also, note that the static main() method should be a member of the Vehicle class. Create a class called Vehicle that has the manufacturers name (type String), number of cylinders in the engine (type int), and owner (type Person given next). Then, create a class called Truck that is derived from Vehicle and has...
Can I please get an in-depth answer to this question: You are an Audit Senior currently...
Can I please get an in-depth answer to this question: You are an Audit Senior currently planning the 30 June 20X9 audits of Comp Limited (Comp), Health Limited (Health), and News Limited (News). At recently scheduled planning meetings with Comp, Health and News, you obtain the following overview of this year’s operations for each of the three client companies: Comp is a manufacturer of computer hardware. The old costing system that was developed inhouse, could no longer keep up with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT