Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program that enters an array of strings
- * and finds in it all sequences of equal elements.
- * The input strings are given as a single line, separated by a space. */
- import java.util.Scanner;
- public class _02_SequencesOfEqualStrings {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter the Elements of your Array on single line, separated by a space:");
- String inputLine = scan.nextLine();
- if (!inputLine.isEmpty()) {
- String[] stringArr = inputLine.split("[\\s]+");
- printAllSequencesOfEqualElements(stringArr);
- } else {
- System.out.println("Error! - Your Array of Elements is Empty!!!");
- }
- }
- private static void printAllSequencesOfEqualElements(String[] strings) {
- System.out.println("The All Sequences of Equal elements are:");
- System.out.print(strings[0]);
- if (strings.length > 1) {
- for (int i = 1; i < strings.length; i++) {
- if (strings[i].equals(strings[i - 1])) {
- System.out.print(" ");
- } else {
- System.out.println();
- }
- System.out.print(strings[i]);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement