0

Example I have this sentence "Hello, my name is Pedro. I want to go to Paris. I'm going to buy the ticket"

I want to separate the text into several sentences where the point appears and then tell which sentence has more words

I came up with this to count the words but I don't know how to separate it into different sentences to count it

public static int countWordsUsingSplit(String parrafo) {

         if (parrafo == null || parrafo.isEmpty()) {
             return 0;
         }
         String[] words = parrafo.split("\\.?!");
         return words.length;

     }

4 Answers4

0
int maxWords = 0;
String maxString;
String[] sentences = new String[parrafo.count(".")];
sentences = parrafo.split(". ");

for(int i = 0; i <= sentences.size(); i++){ //Might as well be .length(), I'm not sure
if(sentences[i].count(" ") + 1 > maxWords){
maxWords = sentences[i].count(" ") + 1;
maxString = sentences[i];
}}
System.out.println(maxString);
Mariot
  • 45
  • 1
  • 6
0

You are on the right track. String.split takes a regular expression (Java details) not literal characters. A dot in a regular expression means "any character", so your split will lots of empty strings, which split supresses, and you get no output.

You need to escape the dot so that it loses its special meaning and you can match on literal dots. You do that by putting a backslash in front of it. But because backslashes are escape characters themselves in Java Strings, you need to escape the backslash with another backslash: "\\.".

Then you split each sentence at white space (\s in regex lingo) and count the words. Add a + after the \s to indicate you want one or more spaces, or use * instead of the + for zero to many (any number).

Here is a complete example:

public class LongestSentenceOfMany {
    public static void main(String[] args) {
        String input = "Hello, my name is Pedro. I want to go to Paris. I'm going to buy the ticket";
 
        // String[] sentences = input.split("\\.");
        String[] sentences = input.split("\\s*\\.\\s*");

        String longestSentence = null;
        int longestCount = -1;
        for (String sentence : sentences) {
            String[] words = sentence.split("\\s+");
            System.out.println("\"" + sentence + "\" has " + words.length + " words.");
            if (longestSentence == null || words.length < longestCount) {
                longestSentence = sentence;
            }
        }
 
        if (longestSentence != null) {
            System.out.println("\"" + longestSentence + "\" is the longest sentence");
        }
    }
}
Robert
  • 7,394
  • 40
  • 45
  • 64
0

You can just use the stream api and some regex magic:

String msg = "Hello, my name is Pedro. I want to go to Paris? I'm going to buy the ticket! ";

Optional<String> result = Arrays.stream(msg.split("(\\. )|(! )|(\\? )"))
        .max((sentence1, sentence2) -> sentence1.length() < sentence2.length() ? -1 : 1);

I am not that good with regex, which means this can likely be improved. This method will not count the ". ", "! ", "? " characters. You can customize the regex to fit your needs. If you wanted to have the smallest sentence just replace the "<" with ">". Here is an amazing site for creating regex.

Sebphil
  • 488
  • 5
  • 12
-2

Hey you can use split as

String[] words = parrafo.split(".");

to only get the sentences seperate by point but you can use your's either to seperate with ? and !.

After that you should look out all the strings that you had in words. To do that you need a loop to travel all. You can use

String maxString=words[0];
int maxLength=words[0].length;

for(String str: words){
   if(str.length > maxLength){
      maxLength=str.length;
      maxString = str;
   }
}

return maxString

We define two variable to keep biggest length of strings for compare and get the string if bigger than maxLength

Hope works

  • 2
    `.split(".")` does _not_ separate the string into sentences. Nor does their `"\\.?!"` split it by questions marks and exclamation marks. – Ivar Jun 25 '21 at 22:51
  • Please, enlighten us @Ivar – Ali Barış Zengin Jun 26 '21 at 03:59
  • 2
    The `.split()` method takes a regular expression as an argument. The dot (`.`) in a regular expression [matches (almost) any character](https://www.regular-expressions.info/dot.html). So `parrafo.split(".")` will split on _every_ character, not literal dots. You will get an array of empty results (the characters between every character). (But due to how `.split()` works, [it will remove empty tailing strings](https://stackoverflow.com/questions/14602062/java-string-split-removed-empty-values), so the result will be an empty array in this case.) I'd recommend testing your solutions. – Ivar Jun 26 '21 at 08:01