0

I have a .txt file with some text in it.

For example Hello, world.

I'd like to search the whole file and find out how many appearances a string has as well as the position of them, For example "wo" on the above text has one. That number should be placed in an edittext. However I only know how to search a specific char and not whole text, can you please help me? Thanks a lot

BufferedReader reader = new BufferedReader(new FileReader("somefile.txt"));
int ch;
char charToSearch='a';
int counter=0;
while((ch=reader.read()) != -1) {
    if(charToSearch == (char)ch) {
        counter++;
    }
};
reader.close();

System.out.println(counter);
gprathour
  • 14,813
  • 5
  • 66
  • 90
Giannis Thanasiou
  • 299
  • 1
  • 4
  • 16
  • possible duplicate: https://stackoverflow.com/questions/5223815/how-do-i-count-the-number-of-times-a-sequence-occurs-in-a-java-string – migos Jul 09 '14 at 16:58

4 Answers4

0

You can use something like:

int nFound = 0;
String target = ".............Your long text..................";
String search = "find this"
int startIndex = 0
do
{
    int index = target.indexOf(search, startIndex);
    if(index !=-1)
    {
        // Found
        nFound++;

        // Here you have the index variable, which says you the position of the found match
        /*  DO your job  */

        /* Update the index to start the search again on the rest of the string, until no matches are found*/
        startIndex = index+1;
    }
    else
        break;


}while(true);

Before doing this, concatenate the whole text in "target" string, or dexecute the previous code for each line if you are sure the target string is not going to appear at the end of some line and the begining of the next line

dhalfageme
  • 1,444
  • 4
  • 21
  • 42
0
public static int countWord(String word, FileInputStream fis) {
    BufferedReader in = new BufferedReader(new InputStreamReader(fis));
    String readLine = "";
    int count = 0;
    try {
        while ((readLine = in.readLine()) != null) {
            String[] words = readLine.split(" ");
            for (String s : words) {
                if (s.contains(word))
                    count++;
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return count;
}
Khanjan
  • 183
  • 8
0

If you are using Java 7, then according to this, you can get a String with the whole file in it:

String text = new String(Files.readAllBytes(Paths.get("file")), StandardCharsets.UTF_8);

Then, you can do this:

public void print(String word)
{
    String tempStr = null;
    int count = 0;
    while (tempStr.indexOf(word) != -1)
    {
        System.out.printf("Position: %d, Count: %d\r\n", tempStr.indexOf(word), ++count);
        tempStr = tempStr.substring(tempStr.indexOf(word) + word.length());
    }
}
Community
  • 1
  • 1
instanceof
  • 1,404
  • 23
  • 30
0

For simplicity, I would read a line and use "string.split(String regex)".

while(readLine) {
  String[] str = readLine.split(regex);
  //you can tell based on the array, how many matches and their position. 
}

You can also use util.Scanner or regex.Pattern.

But if you are looking for performance, I think 'string.indexOf' is the best approach.

Jama Djafarov
  • 358
  • 3
  • 11