Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fourletterwordpercent;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- //TRISTIAN SCHMITZ
- public class FourLetterWordPercent {
- public static void main(String[] args) {
- int totWordCounter = 0;
- int fourLetWordCounter = 0;
- Scanner keyIn = new Scanner(System.in);
- System.out.println("Please enter the name of a text file. "
- + "(including extension)");
- String fileName = keyIn.next();
- File inputFile = new File(fileName);
- try
- {
- Scanner in = new Scanner(inputFile);
- while (in.hasNext())
- {
- if (in.next().length() == 4)
- {
- fourLetWordCounter++;
- }
- totWordCounter++;
- }
- }
- catch (FileNotFoundException e)
- {
- System.out.println("Couldn't find that file. Sorry!");
- }
- System.out.println("The file " + fileName + " contains "
- + (double) fourLetWordCounter / totWordCounter * 100
- + " % four letter words.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement