Recover String between characters

Asked

Viewed 328 times

3

Hello, I’m developing a program that uses the java command line if the user type: send -all < message to be sent > it sends the message to all if send -by < user > < message to be sent > it sends to a specific user. These commands arrive in String, my doubt is how to recover this information and call the specific method, tried with split, but could not. The messages stay between < >.

  • When you say he uses the command line, you mean the command line itself (like: java MinhaClasse send -all < mensagem a ser enviada >) or it reads this command from the standard input (type: System.in)? How’s your code so far?

1 answer

2


Considering the call:

java MeuPrograma -by<usuario> -<mensagem>

You can simply use the parameters sent to the method main:

public class ClasseQuePossuiMetodoPrincipal {
    public static void main(String... args) {
        System.out.println(args[0]); // Vai exibir -by<usuario>
        System.out.println(args[1]); // Vai exibir -<mensagem>
    }
}

Both are delivered to your application as a String, including containing the special characters you sent. To remove them and have a string more friendly, can do so:

public class ClasseQuePossuiMetodoPrincipal {
    public static void main(String... args) {
        System.out.println(clear(args[0])); // Vai exibir byusuario (pode usar um String#substring para remover o 'by' no início)
        System.out.println(clear(args[1])); // Vai exibir mensagem
    }

    private static String clear(String str){
        return str.replaceAll("[^\\dA-Za-z ]", "");
    }
}


Suggested implementation

A better way to do it is by calling your Java application in the following ways:

When you want to send them all: java MeuPrograma "Mensagem que será enviada a todos."
And when you want to send it to a specific user: java MeuPrograma "Olá, como vai?" "foo"

This way, you can check the amount of arguments sent. If sent a single parameter, we consider that the message should be delivered to everyone. Otherwise, if two arguments are sent it means that the message should be sent to a specific user.

Then your class could stay like this:

public class ClasseQuePossuiMetodoPrincipal {
    public static void main(String... args) {
         //Faz aqui algum tratamento caso args.lenght seja zero.
         //E verifica se args.lenght é igual a 2.

        if(args.lenght == 1)
            send(args[0]); // Todos
        else 
            send(args[0], args[1]); // Envia para um usuário
    }

    private static void send(String message){
        // Envia para todos...
    }

    private static void send(String message, String who){
        // Envia para 'who'...
    }
}

Taking advantage of the same idea, if you want to allow the user to use the application by sending the content of the message followed by a user list to which the message should be sent, for example:

java MeuPrograma "Mensagem para os usuários a seguir..." "joao" "mario" "carlos"

Your class could be implemented like this:

import java.util.ArrayList;

public class ClasseQuePossuiMetodoPrincipal {
    public static void main(String... args) {
        //Faz aqui algum tratamento para verificar se args.lenght é igual ou maior que 1.
        String mensagem = args[0];

        if(args.length == 1)
            send(mensagem);

        else {
            ArrayList<String> usuarios = new ArrayList<>();

            // Começando em 1 pois 0 é a mensagem, já armazenada na variável 'mensagem'
            for(int i = 1; i < args.length; i++)
                usuarios.add(args[i]);
            send(mensagem, usuarios);
        }
    }

    private static void send(String message){
        // Envia para todos
    }

    private static void send(String message, ArrayList<String> sendTo){
        // Envia para todos os usuários na lista 'sendTo'
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.