Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class MessageTranslator {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String regex = "!(?<command>[A-Z][a-z]{2,})!:\\[(?<message>[A-Za-z]{8,})\\]";
- Pattern pattern = Pattern.compile(regex);
- int n = Integer.parseInt(scanner.nextLine());
- for (int i = 0; i < n; i++) {
- String input = scanner.nextLine();
- Matcher matcher = pattern.matcher(input);
- if (matcher.find()) {
- String command = matcher.group("command");
- String message = matcher.group("message");
- String[] messageAscii = new String[message.length()];
- for (int j = 0; j < messageAscii.length; j++) {
- messageAscii[j] = "" + (int)message.charAt(j);
- }
- System.out.printf("%s: %s%n", command, String.join(" ", messageAscii));
- } else {
- System.out.println("The message is invalid");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement