/* * Two class. Multiple methods with an array. * Array is passed as a parameter. * * Here the vowels are searched for in a literal string * * Read in the words in the text file into a one dimensional array * Using the words complete the given sentence correctly * "Ahoy Captain a ______ off the port bow." Words starting with a consonants * "Ahoy Captain an ______ off the port bow." Words starting with a vowel * There must be the same amount of sentences as there are words in the text file. */ package ahoycaptain8; public class AhoyCaptain8UI { public static void main(String[]args){ AhoyCaptain8 ahoy = new AhoyCaptain8(); String[] wordArray = new String[20]; wordArray = ahoy.readFile("ahoy.txt"); ahoy.reportOutput(wordArray); } // end main } ================================================================================================= /* * Two class. Multiple methods with an array. * Array is passed as a parameter. * * Here the vowels are searched for in a literal string * * Read in the words in the text file into a one dimensional array * Using the words complete the given sentence correctly * "Ahoy Captain a ______ off the port bow." Words starting with a consonants * "Ahoy Captain an ______ off the port bow." Words starting with a vowel * There must be the same amount of sentences as there are words in the text file. */ package ahoycaptain8; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author seilertsen */ public class AhoyCaptain8 { // Global variables private int size; private String[] wordArray = new String[20]; public String[] readFile(String f) { String fileName = f; size = 0; try { Scanner scFile = new Scanner(new File(fileName)); while (scFile.hasNext()) { String line = scFile.nextLine(); // Only one word per line line = line.toLowerCase(); wordArray[size] = line; size++; } // end while } catch (FileNotFoundException ex) { Logger.getLogger(AhoyCaptain8.class.getName()).log(Level.SEVERE, null, ex); System.out.println("Error file not found"); System.exit(0); } return wordArray; } // end readFile public void reportOutput(String[] wa) { wordArray = wa; // the array of words // a literal list of vowels String vowels = "aeiouAEIOU"; for(int i = 0; i < size; i++) { String firstLetter = wordArray[i].substring(0,1); if(vowels.indexOf(firstLetter) == -1 ) // vowel not found { System.out.println("Ahoy Captain. There is a " + wordArray[i] + " off the port bow."); } else { System.out.println("Ahoy Captain. There is an " + wordArray[i] + " off the port bow."); } } // end inner loop } // end outer loop } // end reportOutput