Advertisement
ivana_andreevska

AV5 Word Count

Aug 10th, 2022
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. package AV5;
  2.  
  3. import javax.sound.sampled.Line;
  4. import java.io.*;
  5. import java.util.Scanner;
  6.  
  7. public class WordCountTest {
  8.     public static void readDataWithScanner(InputStream inputStream) {
  9.         int lines = 0;
  10.         int words = 0;
  11.         int chars = 0;
  12.         Scanner scanner = new Scanner(inputStream);
  13.  
  14.         while (scanner.hasNextLine()) {
  15.             String line = scanner.nextLine();
  16.             ++lines;
  17.             words += line.split("\\s++").length;
  18.             chars += line.length();
  19.         }
  20.         System.out.printf("Lines : %d , Words: %d , Chars: %d", lines, words, chars);
  21.     }
  22.  
  23.     public static void readDataWithBufferedReader(InputStream inputStream) throws IOException {
  24.         int lines=0;
  25.         int words=0;
  26.         int chars=0;
  27.  
  28.         BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
  29.  
  30.         String line;
  31.  
  32.         while((line=bufferedReader.readLine())!=null)
  33.         {
  34.             ++lines;
  35.             words+=line.split("\\s+").length;
  36.             chars+=line.length();
  37.         }
  38.         System.out.printf("Lines : %d , Words: %d , Chars: %d", lines, words, chars);
  39.     }
  40.  
  41.     public static void readDataWithBufferedReaderAndMapReduce(InputStream inputStream)
  42.     {
  43.         BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
  44.         LineCounter result = bufferedReader.lines()
  45.                 //mapiraj od linija vo objekt
  46.                 //za sekoja linija ke se napravi nov line counter
  47.                 .map(l -> new LineCounter(l))
  48.                 //reduce prima 2 raboti->od 10 line counteri napravi 1->od poveke nesta sklopi gi vo edno nesto
  49.                 .reduce(
  50.                         new LineCounter(0, 0, 0),
  51.                         //left e momentot do rezultatot a right posle toa
  52.                         (left, right) -> left.sum(right)
  53.                 );
  54.         //reduce e terminalna akcija
  55.         System.out.println(result);
  56.     }
  57.  
  58.     public static void main(String[] args) throws IOException {
  59.         File file = new File("C:\\Users\\User\\Desktop\\Napredno Sept\\src\\AV5\\dat");
  60.  
  61.         readDataWithScanner(new FileInputStream(file));
  62.         System.out.println();
  63.         readDataWithBufferedReader(new FileInputStream(file));
  64.         System.out.println();
  65.         readDataWithBufferedReaderAndMapReduce(new FileInputStream(file));
  66.     }
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement