Advertisement
risbah

ContainsAllVowels

Jan 28th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.18 KB | None | 0 0
  1. /*## BEGINNER ##
  2. * Write a program that checks if a word contains all the Finnish vowels: a, e, i, o, u, y, Γ€ and ΓΆ
  3.   * The Finnish word list is attached to this post
  4. */
  5. package exercises;
  6.  
  7.  
  8. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.util.ArrayList;
  11. import java.util.Scanner;
  12.  
  13. /*
  14.  * Write a program that checks if a word contains all the vowels
  15.  */
  16.  
  17. public class ContainsAllVowels {
  18.  
  19.     private char[] vowels;
  20.     private String word;
  21.    
  22.     // Constructors
  23.    
  24.     public ContainsAllVowels() {
  25.         this(new char[] {'a','e','i','o','u'}, "arsenious");
  26.     }
  27.    
  28.     public ContainsAllVowels(char[] vowels, String word) {
  29.         this.vowels = vowels;
  30.         this.word = word.toLowerCase().trim();
  31.     }
  32.    
  33.     public ContainsAllVowels(char[] vowels) {
  34.         this(vowels, "");
  35.     }
  36.    
  37.     // Setters
  38.     public void setWord(String word) {
  39.         this.word = word.toLowerCase().trim();
  40.     }
  41.    
  42.     public void setVowelList(char[] vowels) {
  43.         this.vowels = vowels;
  44.     }
  45.    
  46.     public void setVowelList(Scanner scanner) {
  47.         ArrayList<Character> vowels = new ArrayList<>();
  48.         char vowel = ' ';
  49.        
  50.         // Get input
  51.         while(!Character.isDigit(vowel) || !Character.isAlphabetic(vowel)) {
  52.             System.out.print("Enter a digit to quit program \nPlease enter a vowel character: ");
  53.             vowel = scanner.nextLine().charAt(0);
  54.             if(Character.isAlphabetic(vowel)) {
  55.                 vowels.add(vowel);
  56.             }
  57.         }
  58.        
  59.         // Add vowels to this.vowels
  60.         this.vowels = new char[vowels.size()];
  61.         for(int index=0; index<vowels.size();index++) {
  62.             this.vowels[index] = vowels.get(index);
  63.         }
  64.     }
  65.    
  66.     // check if word contains all vowels
  67.     public boolean wordContainsAllVowels(String word) {
  68.        
  69.         int counter = 0, index=0;
  70.        
  71.         if(word.length() < this.vowels.length) {
  72.             return false;
  73.         }
  74.        
  75.         while(counter < this.vowels.length && index < word.length()) {
  76.             if(this.vowels[counter] == word.charAt(index)) {
  77.                 counter++;
  78.                 index = 0;
  79.             }
  80.             else {
  81.                 index++;
  82.             }
  83.         }
  84.         return (counter) == this.vowels.length;
  85.     }
  86.    
  87.     public boolean wordContainsAllVowels() {
  88.         return wordContainsAllVowels(this.word);
  89.     }
  90.    
  91.     // creates list of words that contain all vowels, from .txt file
  92.     private ArrayList<String> createValidWordList(Scanner scanner){
  93.         ArrayList<String> validWords = new ArrayList<>();
  94.        
  95.         String line = scanner.nextLine();
  96.         while(scanner.hasNextLine()) {
  97.             if(wordContainsAllVowels(line)) {
  98.                 validWords.add(line);
  99.             }
  100.             line = scanner.nextLine();
  101.         }
  102.         scanner.close();
  103.        
  104.         return validWords;
  105.     }
  106.    
  107.     public void displayWordsThatContainAllVowels(Scanner scanner) {
  108.         ArrayList<String> words = createValidWordList(scanner);
  109.         System.out.println("Here are the words that contain all vowels: ");
  110.         if(words.size() <= 0) {
  111.             System.out.println("No valid words!");
  112.             return;
  113.         }
  114.        
  115.         for(String w: words) {
  116.             System.out.println(w);
  117.         }
  118.     }
  119.    
  120.     public static void main(String...args) throws FileNotFoundException {
  121.        
  122.         File file = new File("../Practice-Problems-Eclipse/res/wordList.txt");
  123.         Scanner scanner = new Scanner(file, "UTF-8");
  124.         ContainsAllVowels test = new ContainsAllVowels(new char[] {'a', 'e', 'i', 'o', 'u', 'y', 'Γ€', 'ΓΆ'});
  125.         test.displayWordsThatContainAllVowels(scanner);
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement