Count Character Types from a file - Java Algorithm

Java examples for Algorithm:String

Description

Count Character Types from a file

Demo Code

import java.io.*;
import java.util.Arrays;

public class CountCharacterTypes {
    public static void main(String[] args) throws IOException {
        int vowels = 0;
        int consonants = 0;
        int punctuation = 0;
        try (FileReader reader = new FileReader(new File("files/words.txt"));
             PrintWriter writer = new PrintWriter(new FileWriter("files/count-chars.txt"))) {
            int input = reader.read();
            while (input != -1) {
                char ch = (char) input;
                if (Character.isLetter(ch)) {
                    if (Arrays.asList(new Character[]{'a', 'e', 'i', 'o', 'u'}).contains(ch)) {
                        vowels++;/*from  www .  j  a v a  2  s  .c o  m*/
                    } else {
                        consonants++;
                    }
                } else if (Arrays.asList(new Character[]{'!', ',', '.', '?'}).contains(ch)) {
                    punctuation++;
                }

                input = reader.read();
            }

            writer.write(String.format("Vowels: %d\nConsonants: %d\nPunctuation: %d", vowels, consonants, punctuation));

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

Related Tutorials