I have one method {search()} that search for a particular file in a directory that the filename start by "B" and it will return the files. I have 2 file that start by B [Bcc.txt, Ba.txt]. But in main method I want to count a word "light" how many times it appeares in the each files [Bcc.txt, Ba.txt]. However, I really don't know how to do it. I need help on that.
public class CountWord {
public static void main(String[] args) {
System.out.println(search()); //it will print [Bcc.txt, Ba.txt]
}
public static String search() {
File root = new File("/Users");
FilenameFilter beginswithm = new FilenameFilter() {
public boolean accept(File directory, String filename) {
return filename.startsWith("B");
}
};
String[] files = root.list(beginswithm);
if(files==null)
return "No Files";
else
return Arrays.toString(files);
}
}
Code:
package countoccur;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.AbstractMap.SimpleEntry;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CountOccur {
public static void main(String[] args) throws
IOException {
String[] files = search();
String word = "light";
//Read lines and split by space
Files.lines(Paths.get("/Users/" +
files[0])).flatMap(line -> Stream.of(line.split(" ")))
//Convert to lower case each line
.map(String::toLowerCase)
//Filter whether word matches
.filter(w -> w.equalsIgnoreCase(word))
//Count the occureence, just add an entry to map of each
occurrence
.collect(Collectors.toMap(words -> words, words
-> 1, Integer::sum))
//Get the values
.entrySet()
//Create a stream out of it
.stream()
//Count the occurrence
.sorted((a, b) -> a.getValue() == b.getValue() ?
a.getKey().compareTo(b.getKey()) : b.getValue() -
a.getValue())
.forEach(System.out::println);
}
public static String[] search() { // Returning
array to iterate through the file
File root = new
File("/Users/");
FilenameFilter beginswithm = new
FilenameFilter() {
public boolean
accept(File directory, String filename) {
return filename.startsWith("m");
}
};
String[] files =
root.list(beginswithm);
if (files == null)
return null; //
No file found
else
return files; //
returning array of file which contains B
}
}
Sample Output:

Please comment for doubts.
I have one method {search()} that search for a particular file in a directory that the filename s...
Using listAllFiles as a guide, write a method that has one File argument: if the argument is a directory, the method returns the total number of files below it. If the argument represents a file, the method just returns 1. public static int countFiles(File f) import java.io.File; public class FileLister { public static void main(String[] args) { // Choose the directory you want to list. // If running in Eclipse, "." will just be the current project directory. // Use...
Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...
Hi,
So I have a finished class for the most part aside of the toFile
method that takes a file absolute path +file name and writes to
that file. I'd like to write everything that is in my run method
also the toFile method. (They are the last two methods in the
class). When I write to the file this is what I get.
Instead of the desired
That I get to my counsel. I am
having trouble writing my...
How do I correct this error in my code?
When it got to the file type I can only go "null" from a certain
point and I'm wondering how to correct this error.
Also can I get a word document on this coding strategy for this
problem? How much of this am I doing correctly?
The original problem description:
Create a program that provides a listing of all the files in a
directory. The program should be a Java application...
Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...
This is a java assignment Please help me Write the body of the fileAverage() method. Have it open the file specified by the parameter, read in all of the floating point numbers in the file and return their average (rounded to 1 decimal place). For the testing system to work, don't change the class name nor the method name. Additionally, the file will have different contents during testing. public class Main { public double fileAverage( String filename ){ }...
Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.print("There are 5 humans.\n"); array(); } public static String[] array() { //Let the user...
How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please. import java.util.*; public class MergeDemo { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("How many lines to be sorted:"); int size=input.nextInt(); String[] lines=new String[size]; lines[0]=input.nextLine(); System.out.println("please enter lines..."); for(int i=0;i { lines[i]=input.nextLine(); } System.out.println(); System.out.println("Lines Before Sorting:"); System.out.println(Arrays.toString(lines)); mergeSort(lines); System.out.println(); System.out.println("Lines after Sorting:"); System.out.println(Arrays.toString(lines)); } public...
Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...
The method m() of class B overrides the m() method of class A, true or false? class A int i; public void maint i) { this.is } } class B extends A{ public void m(Strings) { 1 Select one: True False For the following code, which statement is correct? public class Test { public static void main(String[] args) { Object al = new AC: Object a2 = new Object(); System.out.println(al); System.out.println(a): } } class A intx @Override public String toString()...