Shyam Bhimani Shyam Bhimani - 1 month ago 9
Java Question

Unable to count vowels using java multithreading

This is what I am trying to do:

enter image description here

CountVowel.java

package lab2;


/**
*
* @author Shyam
*/

public class CountVowel implements Runnable {
String[] input;
int vowels = 0;
char ch;

public CountVowel(String[] args) {
input=args;
}

public void run() {
try {
for (int i = 0; i < input.length; i++) {
String s = input[i];

for (int j = 0; j < s.length(); j++) {
ch = s.charAt(j);

if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o'
|| ch == 'O' || ch == 'u' || ch == 'U')
vowels++;
}
}
System.out.println("Vowels : " + vowels);

} catch (Exception e) {
}
}

}


VowelThread.java

package lab2;


/**
*
* @author Shyam
*/

import java.io.IOException;

public class VowelThread {

public static void main(String[] args)
throws IOException {
for (String str : args) {
System.out.println(str);
}
Thread t1 = new Thread(new CountVowel(args));
t1.start();
}
}


I have tried different approach as below to enter string in cmd however I am not getting desire out put.

enter image description here

enter image description here

C:\Users\Shyam\Documents\NetBeansProjects\lab2\src>java VowelCounter hello hello see you in
Error: Could not find or load main class VowelCounter


C:\Users\Shyam\Documents\NetBeansProjects\lab2\src>javac -cp . lab2\VowelThread.java hello hello see you in london
error: Class names, 'hello,hello,see,you,in,london', are only accepted if annotation processing is explicitly requested
1 error


I would appreciate your help.

Answer

You don't really do what is expected as you don't create a new Thread for each provided argument, indeed you create only one Thread for all the arguments.

To implement it you could use FutureTask and make your class CountVowel implements Callable instead, something like this:

public class CountVowel implements Callable<Integer> {
    private final String input;

    public CountVowel(String input) {
        this.input = input;
    }

    public Integer call() {
        int vowels = 0;
        // Iterate over all the characters of the input String
        for (int j = 0; j < input.length(); j++) {
            // Lower case the current character as we need to ignore the case
            char ch = Character.toLowerCase(input.charAt(j));
            // Use a switch statement instead of an if as it is easier to read and faster
            switch (ch) {
                case 'a':
                case 'e':
                case 'o':
                case 'i':
                case 'u':
                    vowels++;
                    break;
            }
        }
        // Returns the sub total
        return vowels;
    }
}

The main class will then be something like this:

public class VowelThread {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // Create the list that will contain all the tasks
        List<FutureTask<Integer>> futures = new ArrayList<>();
        for (String str : args) {
            // Create a task for the current String
            FutureTask<Integer> future = new FutureTask<>(new CountVowel(str));
            // Add the task to the list of tasks
            futures.add(future);
            // Provide the task to a new Thread and start it
            new Thread(future).start();
        }
        int total = 0;
        // Iterate over all the tasks
        for (FutureTask<Integer> task : futures) {
            // Wait as long as it is needed to get the result of the current task
            total += task.get();
        }
        // Print the total of vowels found
        System.out.printf("Number of vowels: %d%n", total);
    }
}

About the second part of your issue, you need to launch your javac command from the src directory otherwise it cannot compile because your classes are in the package lab2 such that it should be in the directory lab2 from where you launch your javac command.

So assuming that you are in src, the expected commands are the following:

javac lab2\VowelThread.java 
java lab2.VowelThread Hello Hello see you in Italy in Venice

Output:

Number of vowels: 15