Unable to count vowels using java multithreading

Shyam Bhimani

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.

Nicolas Filotto

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find total count of Words, total count of Vowels, total count of Special Character in a text file using java 8

From Dev

Count vowels in a word

From Dev

Count specific vowels in a sentence

From Dev

Printing UI Without Vowels Using Loops Without replaceAll(); in Java

From Dev

Data processing using multithreading java

From Dev

Multithreading help using ExecutorService in Java

From Dev

Count the number of vowels in an array in PHP

From Dev

Printing the vowels and the vowel count in a string

From Dev

Count the number of vowels in an array in PHP

From Dev

Java - Counting the vowels in string

From Dev

Java swap characters but not vowels

From Dev

JAVA: I have to write a program to read "sentences" from a file and and then count the number of words and vowels per sentence

From Java

Initialising your app using multithreading in Java

From Dev

Calling Java from Fortran using JNI, multithreading

From Dev

Creating Dice game in java using Multithreading

From Dev

Multithreading using Blocking IO corrupts file in Java

From Dev

Program to count evens odds vowels and consonants in a string

From Dev

C++ vector and string, count vowels

From Dev

unable to get the row count and access the table data in a table using webdriver 2.0 (Java)

From Dev

Reverse vowels in a string, using regex

From Dev

Java check if parameter contains vowels

From Dev

Reading # of vowels from String in java

From Dev

Use multithreading to count?

From Dev

Java: multithreading

From Dev

Count the number of vowels and print them in a manner that string having more vowels come first

From Dev

Using Java multithreading, what is the most efficient to coordinate finding the best result?

From Dev

I am confused about using static method in Multithreading java?

From Dev

java multithreading using join and handling interrupted execptions correctly

From Dev

bulk of file copy from local to DBFS server using multithreading in java

Related Related

  1. 1

    How to find total count of Words, total count of Vowels, total count of Special Character in a text file using java 8

  2. 2

    Count vowels in a word

  3. 3

    Count specific vowels in a sentence

  4. 4

    Printing UI Without Vowels Using Loops Without replaceAll(); in Java

  5. 5

    Data processing using multithreading java

  6. 6

    Multithreading help using ExecutorService in Java

  7. 7

    Count the number of vowels in an array in PHP

  8. 8

    Printing the vowels and the vowel count in a string

  9. 9

    Count the number of vowels in an array in PHP

  10. 10

    Java - Counting the vowels in string

  11. 11

    Java swap characters but not vowels

  12. 12

    JAVA: I have to write a program to read "sentences" from a file and and then count the number of words and vowels per sentence

  13. 13

    Initialising your app using multithreading in Java

  14. 14

    Calling Java from Fortran using JNI, multithreading

  15. 15

    Creating Dice game in java using Multithreading

  16. 16

    Multithreading using Blocking IO corrupts file in Java

  17. 17

    Program to count evens odds vowels and consonants in a string

  18. 18

    C++ vector and string, count vowels

  19. 19

    unable to get the row count and access the table data in a table using webdriver 2.0 (Java)

  20. 20

    Reverse vowels in a string, using regex

  21. 21

    Java check if parameter contains vowels

  22. 22

    Reading # of vowels from String in java

  23. 23

    Use multithreading to count?

  24. 24

    Java: multithreading

  25. 25

    Count the number of vowels and print them in a manner that string having more vowels come first

  26. 26

    Using Java multithreading, what is the most efficient to coordinate finding the best result?

  27. 27

    I am confused about using static method in Multithreading java?

  28. 28

    java multithreading using join and handling interrupted execptions correctly

  29. 29

    bulk of file copy from local to DBFS server using multithreading in java

HotTag

Archive