Convert String to Array - Java/Groovy

Asked

Viewed 678 times

0

I need to convert a variable string in a array, I tried to do as follows, I have the variable string conteudoLote, where its value is:

[["4","SCAB171301BF5","Vazio","Lexmark International","50F0Z00",null,null,"2017-10-27 08:54:56",false,"ROMUALDO SANTOS"]]

I tried to convert the same to array as follows:

def array = conteudoLote.toCharArray()
for (List listaTeste : array){
    logger.info("NÚMERO DE SÉRIE: "+listaTeste.get(1).toString()) //Aqui serve somente para visualizar o conteúdo do index 1 do meu array
}

However the following error occurred:

Cannot cast object '[' with class 'java.lang.Character' to class 'java.util.List'

How can I convert this string for array then? My need is to get this part of the string SCAB171301BF5 which would be index 1 in the case.

  • You have already turned the String into an array of char. In your for you must exchange List for char.

  • @Leonardolima, how can I access the contents of this mine array like this? Because I need to take this from my string SCAB171301BF5 which would be the index 1 of my array

2 answers

1


If your variable is a string and that is an array:

def arrayString = '''[["4","SCAB171301BF5","Vazio","Lexmark International","50F0Z00",null,null,"2017-10-27 08:54:56",false,"ROMUALDO SANTOS"]]'''

You can give a Eval.me()

def lista = Eval.me(arrayString)

lista[0].each{
    println it
}​

You can try it here in the groovy console

0

I don’t know if this is what you wanted to do, but take a look at the code I generated from your question:

public static void main(String[] args) {
    //teu vetor tem valores null e false (boolean) que não podem estar no vetor
    String[] vetor= {"4","SCAB171301BF5","Vazio","Lexmark International","50F0Z00","2017-10-27 08:54:56","ROMUALDO SANTOS"};

    for (String palavra : vetor) {
        char []letras = palavra.toCharArray();

        for (char letra : letras) {
            System.out.println(letra);
        }
    }
}

Browser other questions tagged

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