java output length of each line in a text document
Java
//Java program to count the number of charaters in a file
import java.io.*;
public class CountCharacters {
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\Mayank\\Desktop\\1.txt");
FileInputStream fileStream = new FileInputStream(file);
InputStreamReader input = new InputStreamReader(fileStream);
BufferedReader reader = new BufferedReader(input);
String line;
//Initializing counters
int countWord = 0;
int sentenceCount = 0;
int characterCount = 0;
int paragraphCount = 1;
int whitespaceCount = 0;
//Reading line by line from the file until a null is returned
while((line = reader.readLine()) != null) {
if(line.equals("")) {
paragraphCount++;
}
if(!(line.equals(""))) {
characterCount += line.length();
// \\s+ is the space delimiter in java
String[] wordList = line.split("\\s+");
countWord += wordList.length;
whitespaceCount += countWord - 1;
//[!?.:]+ is the sentence delimiter in java
String[] sentenceList = line.split("[!?.:]+");
sentenceCount += sentenceList.length;
}
}
System.out.println("Total word count = " + countWord);
System.out.println("Total number of sentences = " + sentenceCount);
System.out.println("Total number of characters = " + characterCount);
System.out.println("Number of paragraphs = " + paragraphCount);
System.out.println("Total number of whitespaces = " + whitespaceCount);
}
}
Also in Java:
- Title
- count number of matches in two strings java
- Category
- Java
- Title
- java shortest if else statement
- Category
- Java
- Title
- how to write sensor data into file android studio
- Category
- Java
- Title
- get value textfield java
- Category
- Java
- Title
- add element to stack java
- Category
- Java
- Title
- convert string to localdatetime
- Category
- Java
- Title
- android create notification
- Category
- Java
- Title
- Unrecognized option: --version Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit.
- Category
- Java
- Title
- how to play a clip of audio in java when needed
- Category
- Java
- Title
- hashmap get value by key java
- Category
- Java
- Title
- java boolean even number
- Category
- Java
- Title
- imagesbutton how to set background
- Category
- Java
- Title
- run java classpath command line
- Category
- Java
- Title
- how to change the icon of a jframe
- Category
- Java
- Title
- compare list from db and list from request
- Category
- Java
- Title
- how to crate a list in java script
- Category
- Java
- Title
- how to take space separated input in java
- Category
- Java
- Title
- android studio centering textview in relativelayout
- Category
- Java
- Title
- java 8 loop in map
- Category
- Java
- Title
- javafx detect collision
- Category
- Java
- Title
- como saber de que tipo es una variable en java
- Category
- Java
- Title
- java fx display simple rectangle
- Category
- Java
- Title
- java getter
- Category
- Java
- Title
- select photo from camera android
- Category
- Java
- Title
- java open file
- Category
- Java
- Title
- javafx textarea size
- Category
- Java
- Title
- Java array hashcode
- Category
- Java
- Title
- processing string to int
- Category
- Java
- Title
- javafx datepicker
- Category
- Java
- Title
- show menuitem when menu hovered javafx
- Category
- Java
- Title
- same method name with different arguments
- Category
- Java
- Title
- java literals w3schools
- Category
- Java
- Title
- java list get first element
- Category
- Java
- Title
- java 8 add whole array to jsonarray
- Category
- Java
- Title
- rock paper scissors java
- Category
- Java
- Title
- how to add integers in java
- Category
- Java
- Title
- JavaFX mouse over effe
- Category
- Java
- Title
- count occurrences of character in string java 8
- Category
- Java
- Title
- connecting to h2 database from java
- Category
- Java
- Title
- Java app development
- Category
- Java
- Title
- string to arraylist convert java
- Category
- Java
- Title
- java jaxb unmarshall xml to map
- Category
- Java
- Title
- actionListener java
- Category
- Java
- Title
- java function for power
- Category
- Java
- Title
- convert hashset to array
- Category
- Java
- Title
- http client java
- Category
- Java
- Title
- a recursive function that calculates the greatest common divisor from user's input in java
- Category
- Java
- Title
- java 8 find in list by property
- Category
- Java
- Title
- java stream sort
- Category
- Java
- Title
- remove part of string java
- Category
- Java
- Title
- remove spaces at beginning and end of string java
- Category
- Java
- Title
- java create folder
- Category
- Java
- Title
- how to see if a shape is touching another shape in java
- Category
- Java
- Title
- print hashtable in java
- Category
- Java
- Title
- float to string java
- Category
- Java
- Title
- how to make javafx full screen default
- Category
- Java
- Title
- how to set frame colo in java
- Category
- Java
- Title
- list java oracle
- Category
- Java
- Title
- foreach java
- Category
- Java
- Title
- how to find the divisors of a number in java
- Category
- Java
- Title
- java null pointer exception
- Category
- Java
- Title
- java pass arraylist by value
- Category
- Java
- Title
- declare array java inline
- Category
- Java
- Title
- iterator loop java
- Category
- Java
- Title
- final variables in java
- Category
- Java
- Title
- hello world in java
- Category
- Java
- Title
- how to create an array in java
- Category
- Java
- Title
- org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder
- Category
- Java
- Title
- how to compare current date and time with another date and time in android
- Category
- Java
- Title
- how to sort collection in java
- Category
- Java
- Title
- java replaceall single character
- Category
- Java
- Title
- string replace last character java
- Category
- Java
- Title
- find duplicate elements in array in java
- Category
- Java
- Title
- while loops java
- Category
- Java
- Title
- capacitor-android. Unfortunately you can't have non-Gradle Java modules and Android-Gradle modules in one project
- Category
- Java
- Title
- how do I change the opacity of a JButton
- Category
- Java
- Title
- java compareto
- Category
- Java
- Title
- java if
- Category
- Java
- Title
- islowercase java
- Category
- Java
- Title
- iterate map in java
- Category
- Java
- Title
- how to detect device javascirpt
- Category
- Java
- Title
- java connect mariadb
- Category
- Java
- Title
- html top padding
- Category
- Java
- Title
- java initialize string array
- Category
- Java
- Title
- jenkins decrypt password script console
- Category
- Java
- Title
- java read public key from keystore
- Category
- Java
- Title
- java script zip function
- Category
- Java
- Title
- java type casting
- Category
- Java
- Title
- expression régulière téléphone java
- Category
- Java
- Title
- fragment manager in android
- Category
- Java
- Title
- how to use an abstract class in java
- Category
- Java
- Title
- how to make a char uppercase in java
- Category
- Java
- Title
- faire un timer en java
- Category
- Java
- Title
- spring code in java
- Category
- Java
- Title
- java http interceptor
- Category
- Java
- Title
- android studio remove notification bar
- Category
- Java
- Title
- java stack peek
- Category
- Java
- Title
- java script using navigator.useragent to detect the browse
- Category
- Java
- Title
- java method
- Category
- Java
- Title
- how to initialize array in java
- Category
- Java