From 4bd4bfee520dab2b55e44287e576fd293a92fa32 Mon Sep 17 00:00:00 2001 From: Jorge Navarro Enamorado <110049286+jorgenavarroenamoradotokio@users.noreply.github.com> Date: Thu, 7 Sep 2023 11:28:34 +0200 Subject: [PATCH] =?UTF-8?q?Reto=20#19=20-=20AN=C3=81LISIS=20DE=20TEXTO=20[?= =?UTF-8?q?Media]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/jorgenavarroenamoradotokio.java" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/java/jorgenavarroenamoradotokio.java" diff --git "a/Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/java/jorgenavarroenamoradotokio.java" "b/Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/java/jorgenavarroenamoradotokio.java" new file mode 100644 index 0000000000..e5e5a50c13 --- /dev/null +++ "b/Retos/Reto #19 - AN\303\201LISIS DE TEXTO [Media]/java/jorgenavarroenamoradotokio.java" @@ -0,0 +1,48 @@ +package com.retos.ej19; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class jorgenavarroenamoradotokio { + + public static void main(String[] args) { + String texto = "Nos encontramos en un periodo de guerra civil. " + + "Las naves espaciales rebeldes, atacando desde una base oculta, " + + "han logrado su primera victoria contra el malvado Imperio Galáctico"; + + System.out.println(obtenerNumeroPalabras(texto)); + System.out.println(obtenerMediaLongitudPalabras(texto)); + System.out.println(obtenerNumeroOraciones(texto)); + System.out.println(obtenerPalabraMayorLongitud(texto)); + } + + private static long obtenerNumeroPalabras(String texto) { + return Stream.of(texto.split("\\s+")).filter(palabra -> !palabra.isEmpty()).count(); + } + + private static int obtenerMediaLongitudPalabras(String texto) { + List palabras = Arrays.stream(texto.split("\\s+")).filter(palabra -> !palabra.isEmpty()) + .collect(Collectors.toList()); + + int longitudTotal = palabras.stream().mapToInt(String::length).sum(); + + return palabras.isEmpty() ? 0 : (int) longitudTotal / palabras.size(); + } + + private static long obtenerNumeroOraciones(String texto) { + return Stream.of(texto.split("\\.")).filter(palabra -> !palabra.isEmpty()).count(); + } + + private static String obtenerPalabraMayorLongitud(String texto) { + List palabras = Arrays.stream(texto.split("\\s+")).filter(palabra -> !palabra.isEmpty()) + .collect(Collectors.toList()); + + Optional max = palabras.stream() + .max((palabra1, palabra2) -> Integer.compare(palabra1.length(), palabra2.length())); + + return max.orElse(""); + } +}