Convert String to Array - Java / Groovy

0

I need to convert a string variable to a array , I tried to do the following, I have the variable string contentLote , where its value is:

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

I tried converting it 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 to array then? My need is to get that part of the string SCAB171301BF5 which would be index 1 in the case.

    
asked by anonymous 27.10.2017 / 13:02

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 Eval.me()

def lista = Eval.me(arrayString)

lista[0].each{
    println it
}​

You can test here on the groovy console

    
30.10.2017 / 19:32
0

I do not 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);
        }
    }
}
    
27.10.2017 / 15:21