How to separate a long string array and print everything

1

I have this arrangement that is returning the string "object" and I'm doing a split in "," .

The idea would be to return each piece of string separated just in "," and that each one has a position in the array with its value, but not all values have "," of separation and I would like to print them separately as well.

For example, in position "0", it prints "BOSS DATA:" along with "Central:" since they are not separated by "," and equal in position 28.

The code I have is the following:

String[] objeto = this.readHTMLData(boss_RP);

for (int i = 0 ; i < objeto.length ; i++) {

    LoggerClass.LogDebugSevice(response.infogenerico.getServiceInfo(), i+" "+objeto[i]);    
}

private String[] readHTMLData(String objeto) {
    String delimitadores = ("\s*,\s*");
    String[] arrayObjetos = objeto.split(delimitadores); 

    return arrayObjetos;  
}   

boss_RP is the full string it brings:

DATOS DE BOSS: 
Central: Trap I, 
IP dslam: 11.111.11.111, 
IP bras: 111.11.1.111, 
COID: 1111, 
Rack: 11, 
SubRack: 11, 
Slot_dslam: 11, 
Port_dslam: 1, 
Dslam_provider: 1111X, 
Community: public, 
Virtual_rack: 11 , 
Virtual_subrack: 11, 
SVLAN: 111, 
CVLAN: 1111, 
Erxslot: 1, 
Erxcard: 1, 
Erxport:1,  
Provider_BRAS: PANCQ, 
Model_BRAS:HT11, 
Nombre_BRAS:cnt-bras-11, 
DSLAMPORTID: 111111 
Datos De Plan:, ABA_N11_1111_BF_N, 1111, 111, GSAF, Filtro_BOSS_Liberado_Navegando 
physical (MODEM)= 1, 
admin (DSLAM)= 1, 
Profile_NUM= 11 --->Invalid Profile 
Velocidades Reales del la Linea (plan): 
DOWN= 111,
 UP = 111 

But when I see the logger it is not showing me the part of the string after each word with ":" .

0 DATOS DE BOSS: 
 Central: 
1 IP dslam: 
2 IP bras: 
3 COID: 
4 Rack: 
5 SubRack: 
6 Slot_dslam: 
7 Port_dslam: 
8 Dslam_provider: 
9 Community: 
10 Virtual_rack:  
11 Virtual_subrack: 
12 SVLAN: 
13 CVLAN: 
14 Erxslot: 
15 Erxcard: 
16 Erxport:
17 Provider_BRAS: 
18 Model_BRAS:
19 Nombre_BRAS:-
20 DSLAMPORTID: 
        <br>
    Datos De Plan:
21 
22 
23 
24 
25      
        <br>
26 physical (MODEM)= 0
27 admin (DSLAM)= 0
28 Profile_NUM=  
        <br>
Velocidades (dsadf): DOWN= 
29  UP =  
    
asked by Miguel C. 08.06.2017 в 15:15
source

2 answers

1

I would do the following:

public static String[] readHTMLData(String objeto) {
    objeto = objeto.replaceAll(",\s*\n", ",\n");
    String[] arrayObjectos = objeto.split("\n");
    return arrayObjectos;
}

This replaces the spaces after commas + line change for a simple line change

objeto.replaceAll(",\s*\n", ",\n")

to then divide the string if there is a line change

objeto.split("\n")

If you print arrayObjectos you get the following:

0 DATOS DE BOSS: 
1 Central: Trap I
2 IP dslam: 11.111.11.111
3 IP bras: 111.11.1.111
4 COID: 1111
5 Rack: 11
6 SubRack: 11
7 Slot_dslam: 11
8 Port_dslam: 1
9 Dslam_provider: 1111X
10 Community: public
11 Virtual_rack: 11 
12 Virtual_subrack: 11
13 SVLAN: 111
14 CVLAN: 1111
15 Erxslot: 1
16 Erxcard: 1
17 Erxport:1
18 Provider_BRAS: PANCQ
19 Model_BRAS:HT11
20 Nombre_BRAS:cnt-bras-11
21 DSLAMPORTID: 111111 
22 Datos De Plan:, ABA_N11_1111_BF_N, 1111, 111, GSAF, Filtro_BOSS_Liberado_Navegando 
23 physical (MODEM)= 1
24 admin (DSLAM)= 1
25 Profile_NUM= 11 --->Invalid Profile 
26 Velocidades Reales del la Linea (plan): 
27 DOWN= 111
28  UP = 111**texto en negrita**

I do not know if that's what you need ...

To print it you could use:

int index = 0;
for (String item : object) {
            System.out.println(index + " " + item);
            index++;
}

Or in the case of using the logger class that you are implementing (I do not know what this LoggerClass class does ... so if it does not work it's a good idea to look at what the LogDebugService method and its parameters do):

int index = 0;
for (String item : object) {
    LoggerClass.LogDebugSevice(response.infogenerico.getServiceInfo(), index +" " + item);
    index++;
}
    
answered by 16.06.2017 / 03:09
source
-1

I think I've understood your problem. I have made a small approximation in which I use an ordered map as a result instead of a [] of strings.

public class TestDeleteMe {
    public static void main(String args[]) {

        // Datos de entrada
        String boss_RP = "DATOS DE BOSS: \r\n"
                + "Central: Trap I, \r\n"
                + "IP dslam: 11.111.11.111, \r\n"
                + "IP bras: 111.11.1.111, \r\n"
                + "COID: 1111, \r\n"
                + "Rack: 11, \r\n"
                + "SubRack: 11, \r\n"
                + "Slot_dslam: 11, \r\n"
                + "Port_dslam: 1, \r\n"
                + "Dslam_provider: 1111X, \r\n"
                + "Community: public, \r\n"
                + "Virtual_rack: 11 , \r\n"
                + "Virtual_subrack: 11, \r\n"
                + "SVLAN: 111, \r\n"
                + "CVLAN: 1111, \r\n"
                + "Erxslot: 1, \r\n"
                + "Erxcard: 1, \r\n"
                + "Erxport:1,  \r\n"
                + "Provider_BRAS: PANCQ, \r\n"
                + "Model_BRAS:HT11, \r\n"
                + "Nombre_BRAS:cnt-bras-11, \r\n"
                + "DSLAMPORTID: 111111 \r\n"
                + "Datos De Plan:, ABA_N11_1111_BF_N, 1111, 111, GSAF, Filtro_BOSS_Liberado_Navegando \r\n"
                + "physical (MODEM)= 1, \r\n" + "admin (DSLAM)= 1, \r\n"
                + "Profile_NUM= 11 --->Invalid Profile \r\n"
                + "Velocidades Reales del la Linea (plan): \r\n"
                + "DOWN= 111,\r\n" + " UP = 111 ";

        // Mapa resultado
        Map<String, String> objeto = readHTMLData(boss_RP);

        // Se recorre la lista y se imprimen los resultados
        int count = 0;      
        for (Entry<String, String> entry : objeto.entrySet()) {
            System.out.println(++count + " " + entry.getKey() + ": "+ entry.getValue());
        }

    }

    /**
     * Lee el objeto pasado por parámetro y extrae la información en forma de mapa
     * @param objeto Cadena de texto con la información a procesar
     * @return Extrae la información procesada en forma de texto
     */
    private static Map<String, String> readHTMLData(String objeto) {

        // Se hace un split sobre el salto de línea
        String[] lines = objeto.split("\r\n");

        // Resultado
        Map<String, String> result = new LinkedHashMap<String, String>();

        // Se recorren la líneas
        for (String line : lines) {
            String[] splittedString = new String[] {};

            // Si la línea contiene : se hace el split por :
            if (line.contains(":")) {
                splittedString = line.split(":");

            // Si la línea contiene = se hace el split por =
            } else if (line.contains("=")) {
                splittedString = line.split("=");
            }

            // Si a lo obtenido tiene más de uno de longitud, significa que al atributo se le asigna un valor
            if (splittedString.length > 1) {

                // Se eliminan las comas

                // [0] Se eliminan los espacios sobrantes
                String realResult = splittedString[1].trim();

                // [1] Se formatean las comas
                if (realResult.endsWith(",")) {
                    realResult = realResult.substring(0,
                            realResult.length() - 1);
                }

                // Se añade el resultado al mapa
                result.put(splittedString[0], realResult);

            // Si a lo obtenido tiene uno de longitud, significa que al atributo no se le asigna un valor
            } else if (splittedString.length == 1) {
                result.put(splittedString[0], "");
            }
        }

        return result;
    }
}

The output on the screen of the code would be a line where attribute appears followed by: and a value, if there is no value, nothing appears. Also you do not see the commas

1 DATOS DE BOSS: 
2 Central: Trap I
3 IP dslam: 11.111.11.111
4 IP bras: 111.11.1.111
5 COID: 1111
6 Rack: 11
7 SubRack: 11
8 Slot_dslam: 11
9 Port_dslam: 1
10 Dslam_provider: 1111X
11 Community: public
12 Virtual_rack: 11 
13 Virtual_subrack: 11
14 SVLAN: 111
15 CVLAN: 1111
16 Erxslot: 1
17 Erxcard: 1
18 Erxport: 1
19 Provider_BRAS: PANCQ
20 Model_BRAS: HT11
21 Nombre_BRAS: cnt-bras-11
22 DSLAMPORTID: 111111
23 Datos De Plan: , ABA_N11_1111_BF_N, 1111, 111, GSAF, Filtro_BOSS_Liberado_Navegando
24 physical (MODEM): 1
25 admin (DSLAM): 1
26 Profile_NUM: 11 --->Invalid Profile
27 Velocidades Reales del la Linea (plan): 
28 DOWN: 111
29  UP : 111
    
answered by 08.06.2017 в 16:35