Friday, February 2, 2018

Longest String in a Sentence in Java


In this article I would like to share with you a sample program that will ask the user to give a string or sentence and then our program will find which of the words in the given string is the longest using Java as our programming language. I am using TextPad as my text editor in writing this program.

I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental is (034) 4335675.





Sample Program Output


Program Listing

string_long.java

import java.util.*;
import java.util.Scanner;

// string_long.java
// Written By Mr. Jake R. Pomperada, MAED-IT
// February 2, 2018   9:59 AM   Friday
// Bacolod City, Negros Occidental Republic of the Philippines
// jakerpomperada@yahoo.com and jakerpomperada@gmail.com

class string_long {

  public static void main(String[] args) {

     String display;

     Scanner input=new Scanner(System.in);

       System.out.println("\n");
   System.out.print("\t Longest String in a Sentence in Java");
   System.out.println("\n");
   System.out.print("Enter a sentence : ");

      String str_sentence =input.nextLine();

     display = Arrays.stream(str_sentence.split(" ")).max(Comparator.comparingInt(String::length)).orElse(null);
    System.out.println("\n\n");
    System.out.println("The longest string is " + display.toUpperCase() + ".");
    System.out.println("\n");
    System.out.println("\t End of Program");
   System.out.println();
  }
}


No comments:

Post a Comment