简体   繁体   中英

I need to count the number of times a substring occurs in a file using scanner and JFIleChooser

I am trying to learn Java and I want to know how I can count the number of times a substring occurs in a chosen text file and have it output to the console. For example say I used JFileChooser to grab a text file from my computer and I wanted to know how many times the substring "if" or "ot" occurred in the file. Any help would be appreciated!

import java.util.Scanner;
import javax.swing.JFileChooser;

public class FileReader {
    public static void main(String[] args) {
        int count = 0;
        JFileChooser chooser = new JFileChooser();
        Scanner in = new Scanner(/* How do I get the file? */);
        { // file read
            while (in.hasNext()) {
                count++;
                in.next();
            }
            System.out.println("The word count is " + count);
        }
    }
}

The first thing you need to do is to actually display the open dialog. You can do this with the method showOpenDialog . Afterwards, you want to get the selected file from the chooser by calling chooser.getSelectedFile() . This file is stored in the variable file .

Now, you need to open the file for reading. In Java you do this with a FileInputStream . You can pass this to the constructor of the class Scanner , together with the character encoding how you expect the file to be encoded.

Now, you have the scanner initialized. To ensure, that all resources are released after you are done, you can use Java's try-with-resource statement, which is of the form

try (/* open resource */) {/* use resource */}

In this case, the scanner is the resource which should be closed. When the scanner is closed, the file is closed.

Then, you only want to count the occurrences of the words "if" and "ot". To do this, you first read the word. Afterwards, check whether it is equal to one of the expected words. If it is, you increase the counter. Please note, that in Java you cannot compare Strings using the == operator.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.swing.JFileChooser;

public class FileReader {
    public static void main(String[] args) throws FileNotFoundException {
        int count = 0;
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File file = chooser.getSelectedFile();
        try (Scanner in = new Scanner(new FileInputStream(file), "UTF-8")) {
            while (in.hasNext()) {
                String token = in.next();
                if (token.equals("if") || token.equals("ot")) {
                    count++;
                }
            }
        }
        System.out.println("The word count is " + count);
    }
}

Instead of handling streams with files you can simply use Apache's StringUtils & FileUtils to count your matching words as like follows,

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.io.FileUtils

import javax.swing.JFileChooser;

public class FileReader 
{
    public static void main(String[] args) 
    {
        int count = 0;
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File file = chooser.getSelectedFile();
        try
        {
            count = StringUtils.countMatches(FileUtils.readFileToString(file),"Search_String or Search_Character");
            count += StringUtils.countMatches(FileUtils.readFileToString(file),"Search_String or Search_Character");
        }
        catch(Exception e)
        {
            System.out.println("Error :" + e);
        }
        System.out.println("The word count is " + count);
    }
}

You can try like this also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM