Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to calculate the number of sentences in text by java

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains how java calculates the number of sentences in the text. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how java calculates the number of sentences in a text.

1. Task introduction

The task of this section is to calculate the number of sentences in the text, and the standard for dividing the number of sentences is "." "!" And "?" Three symbols.

2. Basic tasks and code

1) specific ideas

(1) first of all, I need to use InputStreamReader class and BufferedReader class to read the text. Since the text documents I use are all utf-8 encoded, I also need to specify the encoding format as utf-8.

(2) then you need to define an empty string variable and append the readout to the empty string after reading the text line by line.

(3) read out the document line by line, then use the for loop to traverse each character in the line, use the toString () method to get each character, and then use the if statement and the equals () method to see if the character is "." "!" And "?" One of the three symbols, if it is one of them, the number of sentences + 1

(4) print out the calculated sentence length

2) Task Code

The program is saved as juzi1.java and the code is as follows:

Import java.io.*;// imports all classes in java.io package import java.util.Scanner;// imports Scanner class public class juzi1 {/ / class name public static void main (String [] args) {/ / program main function try {/ / try code block in java.util package. When an exception occurs, it will go to catch code block / / read the specified file Scanner s = new Scanner (System.in) / / to create a scanner, the console will wait for input until the System.out.println ("Please enter the text document you want to open:"); / / enter the prompt information String a = s.nextLine (); / / define a string variable and assign the information entered by the user / / create a class to read the file, and specify the encoding format as utf-8 InputStreamReader read = new InputStreamReader (new FileInputStream (a), "utf-8") BufferedReader in = new BufferedReader (read); / / can be used to read the specified file StringBuffer b = new StringBuffer (); / / define a string variable b to facilitate subsequent content append operations String str = null;// define a string type variable str String d = null;// define a string type variable c int e = 0 / / define an int variable for counting the number of sentences while ((str = in.readLine ())! = null) {/ / readLine () method to read a line and execute b.append (str) all the time as long as the read content is not empty; / / append the contents of the line to the for after the string b (int j = 0; j < str.length () The condition of the {/ / for loop, which loops all the time when j is less than the length of the line and increments d = Character.toString (str.charAt (j)); / / returns a string object if (d.equals (".") | | d.equals ("?") | | d.equals ("!")) {/ / the condition of the if sentence is to determine whether it is a sentence or not / if it is a sentence, e will be incremented once} in.close (); / / close the stream System.out.println ("the text is shared" + e + "sentences"); / / output the total number of sentences} catch (IOException e) {/ / go to the catch code block e.printStackTrace () when there is an exception in the try code block / / printStackTrace () method is to print where and why the exception information went wrong in the program}

At this point, I believe you have a deeper understanding of "how to calculate the number of sentences in java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development