/** * This program displays all of the words in the dictionary ending in "ING" * * @author Dave Slemon * @version v101 */ import java.util.Scanner; import java.io.*; //for IOException, File and FileWriter public class INGwords { //display all the words with "ing" public static void main(String[] args) { //main Scanner dictionary; int count=0; String s=""; String endingStr = ""; String [ ] ingWords = new String[15000]; long start = 0; long finish = 0; long timeElapsed = 0; System.out.println("Starting ... "); start = System.currentTimeMillis(); try { //t dictionary = new Scanner( new File ("words.txt") ); while ( dictionary.hasNextLine() ) {//w s = dictionary.nextLine(); //make sure the word ends in ing if ( s.contains("ing") ) {//s endingStr = s.substring(s.length() - 3) ; if (endingStr.equals("ing")) { System.out.println( s ); ingWords[count] = s; count++; } } //s }//w finish = System.currentTimeMillis(); timeElapsed = finish - start; System.out.println("count = " + count + " time: " + timeElapsed + " msec" ); System.out.println("Finished."); } //t catch (Exception e) {//e System.out.println("Error: file not found."); }//e System.out.println(); //Writing the ingWords to a file called, ingwords.txt try { FileWriter out = new FileWriter("ingwords.txt"); for(int i=0; i< count; i++) { out.write(ingWords[i] + "\n"); } out.close(); System.out.println("Successfully created file ingwords.txt"); } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } } //main }