Q:

Method count words in a file

Hi guys I'm writing a method which counts words in a file, but apparently there is a mistake somewhere in the code and the method does not work. Here's my code:
public class Main2 {

    public static void main(String[] args) {

    count("/home/bruno/Desktop/WAR_JEE_S_09_Podstawy/MojPlik");

    }

    static int count(String fileName){
        Path path = Paths.get(fileName);
        int ilosc = 0;
        String wyjscie = "";
        try {
            for (String charakter : Files.readAllLines(path)){
                wyjscie += charakter;

            }
            StringTokenizer token = new StringTokenizer(wyjscie," \n");

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

        return ilosc;
    }

}

The file path is correct, here is the file content
test test
test
test

after i call the method in main it displays nothing. Where is the mistake ?

A:

Your code would count lines in a file ... well, if you followed up on that thought.
Right ow your code is simply reading lines, putting them into one large string, to then do nothing about the result of that operation. You have a single int counter ... who is init'ed to 0, and then just returned without ever being used/increased! And unless I am mistaken, readAllLines() will automatically remove the newline char in the end, so overall, your code is nothing but useless. 
To count words you have to take each line and (for example) split that one-line-string for spaces. That gives you a number. Then add up these numbers.
Long story short: the real answer here is that you should step back. Don't just write code, assuming that this will magically solve the problem. Instead: first think up a strategy (algorithm) that solves the problem. Write down the algorithm ideas using a pen and paper. Then "manually" run the algorithm on some sample data. Then, in the end, turn the algorithm into code.

