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.ArrayList;
- import java.util.Scanner;
- /**
- *
- * @author Jacob Keenan
- */
- public class FourLetterWordPercent {
- public static void main(String[] args) {
- System.out.println("Please enter the name of the text file.");
- //ArrayList<String> words = new ArrayList<>();
- // ArrayList<String> fourLetters = new ArrayList<>();
- Scanner keyIn = new Scanner(System.in);
- String fileName = keyIn.next();
- File input = new File(fileName + ".txt");
- double count = 0.0;
- double fourLetters = 0.0;
- try
- {
- Scanner list = new Scanner(input);
- while (list.hasNext())
- {
- String word = list.next();
- if(word.length() == 4)
- {
- fourLetters++;
- }
- count++;
- }
- System.out.println(fourLetters + count);
- double total = fourLetters / count * 100;
- System.out.println("The file " + fileName + " contains "
- + total + " % four letter words.");
- list.close();
- }
- catch (FileNotFoundException e)
- {
- System.out.println("Couldn't find that file. Sorry!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement