1 / 4

CLASS EXERCISE

CLASS EXERCISE. Demonstrate processing until EOF Write a program that processes an input file ( story.txt ). It must display the number of vowels in each record. The program must also display final vowel totals after all the lines (i.e. records) have been processed.

danno
Télécharger la présentation

CLASS EXERCISE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CLASS EXERCISE • Demonstrate processing until EOF Write a program that processes an input file (story.txt). It must display the number of vowels in each record. The program must also display final vowel totals after all the lines (i.e. records) have been processed.

  2. CLASS EXERCISE (SOLUTION) import java.io.*; public class CountVowels { public static void main (String[]args) throws IOException { int cntA, cntE, cntI, cntO, cntU; int totA = 0, totE = 0, totI = 0; int totO = 0, totU = 0; char letter; String sentence = null; String fileName = "story.txt"; // declaring input file BufferedReader wordFile = null;

  3. //open input file wordFile = new BufferedReader (new FileReader(inputFileName) ); //priming read sentence = wordFile.readLine(); while (sentence != null) { cntA = 0, cntE = 0, cntI = 0; cntO = 0, cntU = 0; for (int i = 0; i < sentence.length(); i++) { letter = sentence.charAt(i); switch (letter) { case ‘a’: case ‘A’: cntA++; break; case ‘e’: case ‘E’: cntE++; break; case ‘i’: case ‘I’: cntI++; break;

  4. case ‘o’: case ‘O’: cntO++; break; case ‘u’: case ‘U’: cntU++; break; } // end of SWITCH } // end of FOR totA += cntA; totE += cntE; totI += cntI; totO += cntO; totU += cntU; sentence = wordFile.readLine(); //read next } // end of WHILE wordFile.close(); // close file } // end of MAIN

More Related