/* * Two class. Multiple methods with an array. * File name is passed as a parameter * * 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 ahoycaptain6; public class AhoyCaptain6UI { public static void main(String[]args){ AhoyCaptain6 ahoy = new AhoyCaptain6(); ahoy.readFile("ahoy.txt"); ahoy.reportOutput(); } // end main } ========================================================================= /* * Two class. Multiple methods with an array. * File name is passed as a parameter * * 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 ahoycaptain6; 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 AhoyCaptain6 { // Global variables private int size; private String[] wordArray = new String[20]; public void 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(AhoyCaptain6.class.getName()).log(Level.SEVERE, null, ex); } } // end readFile public void reportOutput() { for(int i = 0; i < size; i++) { if(wordArray[i].startsWith("a") || wordArray[i].startsWith("e") ||wordArray[i].startsWith("i") ||wordArray[i].startsWith("o") ||wordArray[i].startsWith("u")) { System.out.println("Ahoy Captain. There is an " + wordArray[i] + " off the port bow."); } else { System.out.println("Ahoy Captain. There is a " + wordArray[i] + " off the port bow."); } } // end for loop } // end reportOutput }