Question

In: Computer Science

using java, parse a text file to be able to list the word(s) with the highest...

using java, parse a text file to be able to list the word(s) with the highest frequency in a sentence across all sentences in the whole file, also print its frequency and the corresponding sentence.

cannot use hash maps.
assume text file will be multiple paragraphs long.

Solutions

Expert Solution

ParseTxtFile.java:

Code:

import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.FileReader;

public class MostRepeatedWord {
public static void main(String[] args) throws Exception {
String paragraph, word = "";
int sCount = 0, maxCount = 0;
ArrayList<String> wordsList = new ArrayList<String>();

//Parsing Text File
FileReader file = new FileReader("inputFile.txt");
BufferedReader br = new BufferedReader(file);

// Each Line Iteration
while((paragraph = br.readLine()) != null) {
String string[] = paragraph.toLowerCase().split("([,.\\s]+)");
for(String s : string){
wordsList.add(s);
}
}

// Finding repeated word
for(int i = 0; i < wordsList.size(); i++){
sCount = 1;
for(int j = i+1; j < wordsList.size(); j++){
if(wordsList.get(i).equals(wordsList.get(j))){
sCount++;
}
}
if(sCount > maxCount){
maxCount = sCount;
word = wordsList.get(i);
}
}

System.out.println("The most repeated word is: " + word);
System.out.println("No of Occurences: " + sCount);
br.close();
}
}

inputFile.txt:

Output:

The most repeated word is: Mobile

No of Occurences: 8


Related Solutions

using java, parse a text file to answer the following question: -list sentences with the maximum...
using java, parse a text file to answer the following question: -list sentences with the maximum number of occurences of the word “the” in the whole file and also list the corresponding frequency. (cannot use hash maps) example output: the:3:The day had came to leave before the storm. What hit the back bumper of the car before the window cracked? The classroom doors where shut closed before the students open the project.
given a input file, parse it and answer the following frequency related questions, using java. -list...
given a input file, parse it and answer the following frequency related questions, using java. -list the most frequent word(s) in the whole file and its frequency. -list sentence(s) with the max. number of occurrences of the word “of” in the entire file and also list the corresponding frequency. program has two arguments; 1st : path to the input text file 2nd : name prefix for the output files ex. $ java assgn1 “./input.txt” “output” outputs: for each question create...
USING JAVA A method that parses the valid word file to a list of Strings. You...
USING JAVA A method that parses the valid word file to a list of Strings. You may use a loop in the parsing method. A nonrecursive method that prints the input String, makes the first call to the recursive anagramizer method, sends the result to the filter method, and prints the filtered result. You may use this code: private void anagramize(String inString) {         System.out.println("input string: " + inString);         List < String > l = filter(anagramizeRecursive(inString.toLowerCase()));         System.out.println("Anagrams: " + l); }
I have a Python code that reads the text file, creates word list then calculates word...
I have a Python code that reads the text file, creates word list then calculates word frequency of each word. Please see below: #Open file f = open('example.txt', 'r') #list created with all words data=f.read().lower() list1=data.split() #empty dictionary d={} # Adding all elements of the list to a dictionary and assigning it's value as zero for i in set(list1):     d[i]=0 # checking and counting the values for i in list1:     for j in d.keys():        if i==j:           d[i]=d[i]+1 #Return all non-overlapping...
How to read a text file and store the elements into a linked list in java?...
How to read a text file and store the elements into a linked list in java? Example of a text file: CS100, Intro to CS, John Smith, 37, 100.00 CS200, Java Programming, Susan Smith, 35, 200.00 CS300, Data Structures, Ahmed Suad, 41, 150.50 CS400, Analysis of Algorithms, Yapsiong Chen, 70, 220.50 and print them out in this format: Course: CS100 Title: Intro to CS Author: Name = John Smith, Age = 37 Price: 100.0. And also to print out the...
java question: How would you be able to store a matrix from a text file into...
java question: How would you be able to store a matrix from a text file into a linked or doubly linked list, if you cannot use 2D arrays? input example: 1 2 3 4 1 3 2 4 4 2 3 1
Using JAVA The following code is able to read integers from a file that is called...
Using JAVA The following code is able to read integers from a file that is called "start.ppm" onto a 3d array called "startImage". Implement the code by being able to read from another file (make up any file name) and save the data onto another 3d array lets say you call that array "finalImage". The purpose of this will be to add both arrays and then get the average Save the average onto a separte 3darray,lets say you call it...
Java Code using Queue Write a program that opens a text file and reads its contents...
Java Code using Queue Write a program that opens a text file and reads its contents into a queue of characters, it should read character by character (including space/line change) and enqueue characters into a queue one by one. Dequeue characters, change their cases (upper case to lower case, lower case to upper case) and save them into a new text file (all the chars are in the same order as the original file, but with different upper/lower case) use...
Java Code using Stack Write a program that opens a text file and reads its contents...
Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
Using JAVA Resource: "Analyze and Document JDBC API Calls" text file ** PASTED BELOW** For this...
Using JAVA Resource: "Analyze and Document JDBC API Calls" text file ** PASTED BELOW** For this assignment, you will analyze code that uses the JDBC API to access a database, retrieve data, and compose output based on that data. You will then comment the code to reflect the purpose and expected results of the code. Download the linked TXT file, and read through the Java™ code carefully. Add your name, instructor's name, and today's date to the header comment. Replace...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT