Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1.  
  2. package fourletterwordpercent;
  3.  
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.util.ArrayList;
  7. import java.util.Scanner;
  8.  
  9. /**
  10. *
  11. * @author Jacob Keenan
  12. */
  13. public class FourLetterWordPercent {
  14.  
  15. public static void main(String[] args) {
  16. System.out.println("Please enter the name of the text file.");
  17. //ArrayList<String> words = new ArrayList<>();
  18. // ArrayList<String> fourLetters = new ArrayList<>();
  19.  
  20. Scanner keyIn = new Scanner(System.in);
  21. String fileName = keyIn.next();
  22. File input = new File(fileName + ".txt");
  23. double count = 0.0;
  24. double fourLetters = 0.0;
  25. try
  26. {
  27. Scanner list = new Scanner(input);
  28.  
  29. while (list.hasNext())
  30. {
  31. String word = list.next();
  32. if(word.length() == 4)
  33. {
  34. fourLetters++;
  35. }
  36. count++;
  37. }
  38. System.out.println(fourLetters + count);
  39. double total = fourLetters / count * 100;
  40. System.out.println("The file " + fileName + " contains "
  41. + total + " % four letter words.");
  42. list.close();
  43. }
  44. catch (FileNotFoundException e)
  45. {
  46. System.out.println("Couldn't find that file. Sorry!");
  47. }
  48. }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement