Advertisement
dimipan80

2. Sequences of Equal Strings

Sep 12th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. /* Write a program that enters an array of strings
  2.  * and finds in it all sequences of equal elements.
  3.  * The input strings are given as a single line, separated by a space. */
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class _02_SequencesOfEqualStrings {
  8.  
  9.     public static void main(String[] args) {
  10.         // TODO Auto-generated method stub
  11.         Scanner scan = new Scanner(System.in);
  12.         System.out.println("Enter the Elements of your Array on single line, separated by a space:");
  13.         String inputLine = scan.nextLine();
  14.  
  15.         if (!inputLine.isEmpty()) {
  16.             String[] stringArr = inputLine.split("[\\s]+");
  17.             printAllSequencesOfEqualElements(stringArr);
  18.         } else {
  19.             System.out.println("Error! - Your Array of Elements is Empty!!!");
  20.         }
  21.     }
  22.  
  23.     private static void printAllSequencesOfEqualElements(String[] strings) {
  24.         System.out.println("The All Sequences of Equal elements are:");
  25.         System.out.print(strings[0]);
  26.         if (strings.length > 1) {
  27.             for (int i = 1; i < strings.length; i++) {
  28.                 if (strings[i].equals(strings[i - 1])) {
  29.                     System.out.print(" ");
  30.                 } else {
  31.                     System.out.println();
  32.                 }
  33.  
  34.                 System.out.print(strings[i]);
  35.             }
  36.         }
  37.     }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement