-2

the task is to find number of occurrences of a particular word in a file that person wrote herself.

public void reader() {
    BufferedReader myR = myReader("enter the name of a file: ");
    int count = 0;
    String substring = readLine("enter the string to count for entry: ");
    try {
        while (true) {
            String s = null;
            s = myR.readLine();
            if (s == null)
                break;
            for(int j=0; j<s.length(); j++){
                if(s.contains(substring)){
                    count++;
                }
            }
        }
        myR.close();
    } catch (IOException e) {
        throw new ErrorException(e);
    }
    println("number of words is: " + count);
}

private BufferedReader myReader(String prompt) {
    BufferedReader rd = null;
    while (rd == null) {
        try {
            String name = readLine(prompt);
            rd = new BufferedReader(new FileReader(name));
        } catch (FileNotFoundException e) {
            println("wrong file entered");
            // e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    return rd;
}

So the problem is that i can't figure out what to do if in my text file number of word i was checking is 4, but the code prints 671

Student
  • 107
  • 4

2 Answers2

0

the problem lies in this loop:

while (true) {
        String s = null;
        s = myR.readLine();
        if (s == null)
            break;
        for(int j=0; j<s.length(); j++){
            if(s.contains(substring)){
                count++;
            }
        }
    }

now suppose your bufferedReader reads a line "hie i am user".

the size of this string is 13 so string.length(); would return 13.

that means you would be checking the same line for 13 iterations for your match.

So, suppose if you are looking for a match say "user" then checking for "user" on the same line for 13 times would make your count go up to 13.

you can replace the above code with this code:

while (true) {
        String s = null;
        s = myR.readLine();
        if (s == null)
            break;
        String[] slist = s.split(" ");
        for(int j=0; j<slist.length(); j++){
            if(slist[j].contains(substring)){
                count++;
            }
        }
    }
AppleCiderGuy
  • 1,249
  • 1
  • 9
  • 16
  • Thanks a lot, but the problem is that i can't use an array in this task – Student Apr 30 '17 at 07:05
  • @Student find the index of the first occurrence of the substring. If found, increment count, and find the **next** occurrence of the substring (i.e. starting from the end of the first occurrence), etc. – JB Nizet Apr 30 '17 at 07:10
0

ohh!! you should have mentioned that you wanna do it without using an array.

this snippet should help you:

while (true) {
        String s = null;
        s = myR.readLine();
        if (s == null)
            break;
        for(int j=0; j<s.length(); j++){
            if(s.equals(" ")){
                String temp = s.substring(j+1, s.length());
                String word = temp.substring(0,temp.indexOf(" ")-1);
                if(temp.equalsIgnoringCases(word)){
                   count++;  
                }
            }
        }
    }

now what i am doing here is first of all i am looking for a space in the whole string and upon finding one, I am extracting a substring starting from the index next to the index of space to the end of the string.

Now from this extracted substring, I am further extracting a substring from index zero up till the first space. this string is essentially a word suitable for comparison.

AppleCiderGuy
  • 1,249
  • 1
  • 9
  • 16