skip to Main Content

I’m trying to extract values from a text file. The content of the file is as follows:

Cliente

1, 'Aarón', 'Rivero', 'Gómez', 'Almería', 100
2, 'Adela', 'Salas', 'Díaz', 'Granada', 200
3, 'Adolfo', 'Rubio', 'Flores', 'Sevilla', NULL
4, 'Adrián', 'Suárez', NULL, 'Jaén', 300
5, 'Marcos', 'Loyola', 'Méndez', 'Almería', 200
6, 'María', 'Santana', 'Moreno', 'Cádiz', 100
7, 'Pilar', 'Ruiz', NULL, 'Sevilla', 300
8, 'Pepe', 'Ruiz', 'Santana', 'Huelva', 200
9, 'Guillermo', 'López', 'Gómez', 'Granada', 225
10, 'Daniel', 'Santana', 'Loyola', 'Sevilla', 125

I’ve managed to read the file and get the lines, but I can’t specify the exact lines that start with digits. I want to split the values and add them to an SQL table. Using line.startsWith("^[0-9].*") doesn’t seem to work, and I’m out of ideas. I tried using a matcher to match the line, and it doesn’t work either.

2

Answers


  1. Chosen as BEST ANSWER
    public static void main(String[] args) throws IOException {
        try {
            String line;
            String currentSection = null;
    
            BufferedReader reader = new BufferedReader(new FileReader("C:\Users\nombr\Downloads\datos_ventas.txt"));
    
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                if (line.startsWith("Cliente")) {
                    
                    currentSection = "Clientes";
                    
                    if (line.startsWith("^[0-9].*")){
                        
                        String[] valores = line.split(",");
                        if (valores != null){
                            String valor1 = valores[0];
                            String valor2 = valores[1];
                            String valor3 = valores[2];
                            String valor4 = valores[3];
                            String valor5 = valores[4];
                            String valor6 = valores[5];
                            System.out.print("Id : " + valor1);
                            System.out.println("Nombre : " + valor2);
                            System.out.println("Apellidos : " + valor3 + " " + valor4);
                            System.out.println("Vivienda : " + valor5);
                            System.out.println("Precio : " + valor6);
                            
                            
                        }
                    }
                }
            }
    

  2. this code worked for me

    public class Main {
    
    
    public static String[][] arrayedDatas(String data){
        String[] datas = data.split(",");
        
        int sizeOfData = datas.length/5;
        
        String[][] arrayed = new String[sizeOfData][5];
        
        int j = 0;
        for(int i=0; i<arrayed.length; i++) {
            arrayed[i][0] = datas[j].trim();
            arrayed[i][1] = datas[j+1].trim();
            arrayed[i][2] = datas[j+2].trim();
            arrayed[i][3] = datas[j+3].trim();
            arrayed[i][4] = datas[j+4].trim();
            j += 5;
        }
        
        return arrayed;
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        String datas = "1, 'Aarón', 'Rivero', 'Gómez', 'Almería', 100 2, 'Adela', 'Salas', 'Díaz', 'Granada', 200 3, 'Adolfo', 'Rubio', 'Flores', 'Sevilla', NULL 4, 'Adrián', 'Suárez', NULL, 'Jaén', 300 5, 'Marcos', 'Loyola', 'Méndez', 'Almería', 200 6, 'María', 'Santana', 'Moreno', 'Cádiz', 100 7, 'Pilar', 'Ruiz', NULL, 'Sevilla', 300 8, 'Pepe', 'Ruiz', 'Santana', 'Huelva', 200 9, 'Guillermo', 'López', 'Gómez', 'Granada', 225 10, 'Daniel', 'Santana', 'Loyola', 'Sevilla'";
        
        String[][] arrayed = arrayedDatas(datas);
        
        for(String[] a : arrayed) {
            for(String b : a) {
                System.out.println(b);
            }
            System.out.println("===========");
        }
        
     }
    
    }
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search