Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*## BEGINNER ##
- * Write a program that checks if a word contains all the Finnish vowels: a, e, i, o, u, y, Γ€ and ΓΆ
- * The Finnish word list is attached to this post
- */
- package exercises;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Scanner;
- /*
- * Write a program that checks if a word contains all the vowels
- */
- public class ContainsAllVowels {
- private char[] vowels;
- private String word;
- // Constructors
- public ContainsAllVowels() {
- this(new char[] {'a','e','i','o','u'}, "arsenious");
- }
- public ContainsAllVowels(char[] vowels, String word) {
- this.vowels = vowels;
- this.word = word.toLowerCase().trim();
- }
- public ContainsAllVowels(char[] vowels) {
- this(vowels, "");
- }
- // Setters
- public void setWord(String word) {
- this.word = word.toLowerCase().trim();
- }
- public void setVowelList(char[] vowels) {
- this.vowels = vowels;
- }
- public void setVowelList(Scanner scanner) {
- ArrayList<Character> vowels = new ArrayList<>();
- char vowel = ' ';
- // Get input
- while(!Character.isDigit(vowel) || !Character.isAlphabetic(vowel)) {
- System.out.print("Enter a digit to quit program \nPlease enter a vowel character: ");
- vowel = scanner.nextLine().charAt(0);
- if(Character.isAlphabetic(vowel)) {
- vowels.add(vowel);
- }
- }
- // Add vowels to this.vowels
- this.vowels = new char[vowels.size()];
- for(int index=0; index<vowels.size();index++) {
- this.vowels[index] = vowels.get(index);
- }
- }
- // check if word contains all vowels
- public boolean wordContainsAllVowels(String word) {
- int counter = 0, index=0;
- if(word.length() < this.vowels.length) {
- return false;
- }
- while(counter < this.vowels.length && index < word.length()) {
- if(this.vowels[counter] == word.charAt(index)) {
- counter++;
- index = 0;
- }
- else {
- index++;
- }
- }
- return (counter) == this.vowels.length;
- }
- public boolean wordContainsAllVowels() {
- return wordContainsAllVowels(this.word);
- }
- // creates list of words that contain all vowels, from .txt file
- private ArrayList<String> createValidWordList(Scanner scanner){
- ArrayList<String> validWords = new ArrayList<>();
- String line = scanner.nextLine();
- while(scanner.hasNextLine()) {
- if(wordContainsAllVowels(line)) {
- validWords.add(line);
- }
- line = scanner.nextLine();
- }
- scanner.close();
- return validWords;
- }
- public void displayWordsThatContainAllVowels(Scanner scanner) {
- ArrayList<String> words = createValidWordList(scanner);
- System.out.println("Here are the words that contain all vowels: ");
- if(words.size() <= 0) {
- System.out.println("No valid words!");
- return;
- }
- for(String w: words) {
- System.out.println(w);
- }
- }
- public static void main(String...args) throws FileNotFoundException {
- File file = new File("../Practice-Problems-Eclipse/res/wordList.txt");
- Scanner scanner = new Scanner(file, "UTF-8");
- ContainsAllVowels test = new ContainsAllVowels(new char[] {'a', 'e', 'i', 'o', 'u', 'y', 'Γ€', 'ΓΆ'});
- test.displayWordsThatContainAllVowels(scanner);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement