Advertisement
ppathak35

STring Lines

Jun 24th, 2022
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class Main{
  5.     public static void main(String args[]) throws Exception{
  6.         File file = new File("hello.txt");
  7.         Scanner sc = new Scanner(file);
  8.  
  9.         int numLines=0, numWords=0, numChars=0;
  10.         String line = "";
  11.         String[] words;
  12.         while (sc.hasNextLine()){
  13.             line = sc.nextLine();
  14.             System.out.println(line);
  15.             numLines += 1;
  16.             numChars += line.length();
  17.             words = line.split("\\s");
  18.             numWords += words.length;
  19.         }
  20.         System.out.println("Characters : " + numChars);
  21.         System.out.println("Words : " + numWords);
  22.         System.out.println("Lines : " + numLines);
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement