Robert
Robert

Reputation: 43

Taking first uppercase character of a multiple splitted string

So I want to print out the first uppercase character when splitting my String when it reaches a special character.

public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    String input = sc.nextLine();
    if(input.contains("-")){
        for (int i = 0; i < input.length(); i++){
            String[] parts = input.split("-",2);
            String string1 = parts[0];
            String string2 = parts[1];
            System.out.print(string1.substring(0, 0) + string2.substring(0,0));
            
        }
    }
}

I'll give an example of what I'd like it to do.

> input: Please-WooRk-siR
> output: PW
> input: This-Is-A-Test
> output: TIAT

So only print the first uppercase character of each substring.

Upvotes: 1

Views: 103

Answers (4)

bastian
bastian

Reputation: 106

Another solution by utilizing javas streaming API

    Scanner sc = new Scanner(System.in);
    String input = sc.nextLine();
    if (input.contains("-")) {
        List<String> collect =
                Stream.of(input.split("-")) // get a stream of words
                .map(word -> word.substring(0, 1)) // get first character only
                .filter(character -> character.toUpperCase().equals(character)) // only keep if character is uppercase
                .peek(System.out::print) // print out character
                .collect(Collectors.toList()); // just needed for stream to start
    }
}

Upvotes: 0

Anish B.
Anish B.

Reputation: 16559

Update the code to this :

import java.util.*;
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        if (input.contains("-")) {
            String[] parts = input.split("-");
            for (String part: parts) {
                System.out.print(Character.isUpperCase(part.charAt(0)) ? part.charAt(0) : "");
            }

        }
    }
}

Output :

1.

Input : A-Ba

AB

2.

Input : A-aB

A

3.

Input : A

Now, your test case :

Input : This-Is-A-Test

TIAT

Upvotes: 1

WJS
WJS

Reputation: 40057

This is one way to do it.

      String str = "This-Is-a-Test-of-The-Problem";
      StringBuilder sb = new StringBuilder();
      for (String s : str.split("-")) {
         char c = s.charAt(0);
         if (Character.isUpperCase(c)) {
            sb.append(c);
         }
      }
      System.out.println(sb.toString());

Upvotes: 1

ᴇʟᴇvᴀтᴇ
ᴇʟᴇvᴀтᴇ

Reputation: 12781

If you use regular expressions, you can use a zero-width negative lookahead to remove all characters that aren't capitals at the starts of words:

public static String capitalFirstLetters(String s) {
    return s.replaceAll("(?!\\b[A-Z]).", "");
}

When you run the test cases:

public static void main(String[] args) throws Exception {
    System.out.println(capitalFirstLetters("Please-WooRk-siR"));
    System.out.println(capitalFirstLetters("This-Is-A-Test"));
}

It prints:

PW
TIAT

Upvotes: 3

Related Questions