Advertisement
Guest User

Untitled

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