Advertisement
ivana_andreevska

AV5 Word Count (File)

Aug 10th, 2022
921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. package AV5;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.InputStream;
  7. import java.util.Scanner;
  8.  
  9. public class WordCountTest {
  10.     public static void readDataWithScanner(InputStream inputStream) {
  11.         int lines = 0;
  12.         int words = 0;
  13.         int chars = 0;
  14.         Scanner scanner = new Scanner(inputStream);
  15.  
  16.         while (scanner.hasNextLine()) {
  17.             String line = scanner.nextLine();
  18.             ++lines;
  19.             words += line.split("\\s").length;
  20.             chars += line.length();
  21.         }
  22.         System.out.printf("Lines : %d , Words: %d , Chars: %d", lines, words, chars);
  23.     }
  24.  
  25.     public static void main(String[] args) throws FileNotFoundException {
  26.         File file=new File("C:\\Users\\User\\Desktop\\Napredno Sept\\src\\AV5\\dat");
  27.  
  28.         readDataWithScanner(new FileInputStream(file));
  29.     }
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement