deadpixels
deadpixels

Reputation: 809

How to find on which line a word is in Java

I am trying to create a program that counts the number of times a word appears in a text and also tell you how many times it appears on each line. I have managed to find the number of times the word appears and the number of lines in the text, but I cannot find on which line the word appears in and how many times. Could you please help me? This is my code so far:

    FileReader file = new FileReader("C:/Users/User/Desktop/test.txt");
    BufferedReader buffer = new BufferedReader(file);

    String line = buffer.readLine();
    Map<String, Integer> hash = new HashMap<String, Integer>();
    int counter = 0; //number of lines

    while (line != null){
        String[] words = line.split(" ");

        for (String s : words) {
            Integer i = hash.get(s);
            hash.put(s, (i==null)? 1: i+1);
        }

        line = buffer.readLine();
        counter = counter + 1;           
    }

System.out.println(hash);
System.out.println(counter);

Upvotes: 1

Views: 2353

Answers (2)

alainlompo
alainlompo

Reputation: 4434

Here is a recursive method that will allow you, using String.indexOf to count how many times a word appears in a line.

You have read the line from your bufferedReader

String line = buffer.readLine();

then in your loop you have

 for (String s : words) {
    int numberOfOccurencesOfS = countNumberOfTimesInALine(line,s);        
 } 

the countNumberOfTimesInALinereceives the original line and the word your are counting as arguments. To use it you should also declare a class variable like this: private static int numberOfLineOccurences;

Here is the method

public static int countNumberOfTimesInALine(String line, String word) {
    if (line.indexOf(word) == -1) {
        return numberOfLineOccurences;
    } else {
        numberOfLineOccurences++;
        if (line.indexOf(word) + word.length() >  line.length() -1     ) {
            return numberOfLineOccurences;
        }
        return countNumberOfTimesInALine(
                line.substring(line.indexOf(word) + word.length()), word );
    }

}

Here is a usage example:

String line = "DEMO TEST DEMO TEST DEMO TEST ALPHA BETA GAMMA";
System.out.println("Number of occurences of TEST is " + countNumberOfTimesInALine(line, "TEST"));

Here is the result

Number of occurences of TEST is 3

I have published an answer to a similar question as yours here

Upvotes: 0

libik
libik

Reputation: 23029

It is additional information to each row. You just need an information of count on each line, therefore simple Map is not enough, you need Map of Map at each row.

There are two basic ways :

    Map<Integer, Map<String, Integer>> hashOfHash = new HashMap<>();
    List<Map<String, Integer>> list = new ArrayList<>();

First line creates Map of your Map based on integer key value - which would be the line.

Second line is creating list of your Maps, because the order in list is stored, you can now which line is which just by iterating through it.

I would recommend second line.

You need also modify your while cycle a bit to be able to create new map for each line (think about it that you need to do the same as it does at first line).


For example this should do the same as your program, but it will show results for each row :

public static void main(String[] args) throws FileNotFoundException, IOException {
    FileReader file = new FileReader("C:/Users/User/Desktop/test.txt");
    BufferedReader buffer = new BufferedReader(file);

    String line = buffer.readLine();

    List<Map<String, Integer>> list = new ArrayList<>();

    while (line != null) {
        Map<String, Integer> hash = new HashMap<String, Integer>();
        String[] words = line.split(" ");

        for (String s : words) {
            Integer i = hash.get(s);
            hash.put(s, (i == null) ? 1 : i + 1);
        }

        line = buffer.readLine();
        list.add(hash);
    }

    int i=0;
    for (Map<String, Integer> mapAtRow : list) {
        i++;
        System.out.println("at row " + i + "we found this: " + mapAtRow);
    }
}

Upvotes: 1

Related Questions