In: Computer Science
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.
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.