File reading

0

I am trying to make a program that reads a text file, it must be printed on screen in some defined fields, I must configure a button of first, next, previous and last. I'm with the next but I try to do it through a cycle as if it were an array but I can not get ahead every time I press the button.  public static void main (String [] args) {         JFrame v_frame = new JFrame ();         v_frame.setSize (500, 500);         v_frame.setLayout (null);

    JLabel lb_nombre = new JLabel("Nombre:");
    lb_nombre.setBounds(50, 50, 80, 30);
    v_frame.add(lb_nombre);

    JLabel lb_cedula = new JLabel("Cedula:");
    lb_cedula.setBounds(50, 100, 100, 30);
    v_frame.add(lb_cedula);

    JLabel lb_edad = new JLabel("Edad:");
    lb_edad.setBounds(50, 150, 200, 30);
    v_frame.add(lb_edad);

    v_nombre.setBounds(150, 50, 200, 30);
    v_frame.add(v_nombre);

    v_cedula.setBounds(150, 100, 200, 30);
    v_frame.add(v_cedula);

    v_edad.setBounds(150, 150, 200, 30);
    v_frame.add(v_edad);

    v_modelo.addColumn("Nombre: ");
    v_modelo.addColumn("Cedula: ");
    v_modelo.addColumn("Edad: ");
    v_tabla.setBounds(50, 250, 400, 140);
    v_tabla.setVisible(true);
    v_frame.add(v_tabla);

    JButton bt_salir = new JButton();
    bt_salir.setText("Salir");
    bt_salir.setBounds(400, 400, 80, 30);
    bt_salir.setVisible(true);
    v_frame.add(bt_salir);
    bt_salir.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);

        }
    });

    JButton bt_primero = new JButton();
    bt_primero.setText("Primero");
    bt_primero.setBounds(20, 200, 80, 30);
    bt_primero.setVisible(true);
    v_frame.add(bt_primero);
    bt_primero.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            leer();
            primero(v_modelo);

        }
    });

    JButton bt_Siguiente = new JButton();
    bt_Siguiente.setText("Siguiente");
    bt_Siguiente.setBounds(110, 200, 100, 30);
    bt_Siguiente.setVisible(true);
    v_frame.add(bt_Siguiente);
    bt_Siguiente.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            leer();
            siguiente();

        }
    });

    JButton bt_ante = new JButton();
    bt_ante.setText("Anterior");
    bt_ante.setBounds(220, 200, 100, 30);
    bt_ante.setVisible(true);
    v_frame.add(bt_ante);
    bt_ante.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            leer();

        }
    });

    JButton bt_ulti = new JButton();
    bt_ulti.setText("Ultimo");
    bt_ulti.setBounds(330, 200, 100, 30);
    bt_ulti.setVisible(true);
    v_frame.add(bt_ulti);
    bt_ulti.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            leer();

        }
    });

    v_frame.setVisible(true);
}

Code for the following button:  public static void next () {

    try {
        FileReader v_lector = new FileReader(v_ruta_archivo);
        BufferedReader mi_buffer = new BufferedReader(v_lector);
        String v_linea;
        while ((v_linea = mi_buffer.readLine()) != null) {
            String v_arreglo[] = v_linea.split(",");
            Object[] v_temporal = {v_arreglo[0], v_arreglo[1], v_arreglo[2]};
            int lim = v_arreglo.length;
            for (int i = 0; i < lim - 2; i++) {
                for (int j = 0; j < i + 1; j++) {
                    String linea1 = (String) v_modelo.getValueAt(i, 0);
                    String linea2 = (String) v_modelo.getValueAt(i, 1);
                    String linea3 = (String) v_modelo.getValueAt(i, 2);
                    v_nombre.setText(linea1);
                    v_cedula.setText(linea2);
                    v_edad.setText(linea3);

                }
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "Error de Lectura", "Error", 0);
    }
}

}

I wanted to use the file as if it were an array, or trying to transform it from a JTable to an Array for handling, but I'm lost, some suggestions please. Thank you

    
asked by Felix mejias 21.11.2018 в 22:05
source

2 answers

1

My suggestion would be to read txt file:

public Stream<String> leerAchivoExterno(String rutaArchivo) {
    Stream<String> lineas = null;

    try {
        lineas = Files.lines(Paths.get(rutaArchivo), Charset.forName("UTF-8"));
    } catch (IOException ex) {
        Logger.getLogger(ArchivoImpl.class.getName()).log(Level.SEVERE, null, ex);
    }

    return lineas;
}

and from here what you can do is use the split command ("separator") to have all the values of each line of the txt and turn it into an array. Here I leave an ejm. from another user with a similar doubt.

public String[][] texto2arreglo(Stream<String> archivo){
    List<String> textos = archivo.collect(Collectors.toList()); // se convierte el stream en una lista

    int filas = textos.size(); // se obtiene la cantidad de filas necesarias
    int columnas = textos.get(0).split(",").length; // se obtienen las columnas necesarias separando el contenido por las comas

    String arreglo[][] = new String[filas][columnas]; // se establece un arreglo

    IntStream.range(0, filas).forEach(y -> { // se recorre fila a fila del archivo
        String linea = textos.get(y);
        IntStream.range(0, columnas).forEach(x -> { // y por cada fila se recorre el contenido que se encuentra separado por comas correspondiente a cada columna
            arreglo[y][x] = linea.split(",")[x]; // se guarda en el arreglo el contenido que le corresponde en la fila y columna de interes
        });
    });

   return arreglo;
}
    
answered by 21.11.2018 в 22:18
0

Good I found another way to be able to realize part of what I need but I have another problem:

public static void siguiente() {
    int tam = v_modelo.getDataVector().size();
    int cont = 0;
    if (cont<tam-1) {
        String linea1 = (String) v_modelo.getValueAt(cont, 0);
        String linea2 = (String) v_modelo.getValueAt(cont,1);
        String linea3 = (String) v_modelo.getValueAt(cont, 2);
        v_nombre.setText(linea1);
        v_cedula.setText(linea2);
        v_edad.setText(linea3);
        cont++;
        System.out.println("contador"+cont);

        }
    }
}

Here I jump from one to the last value and do not give it to me one by one as I need it. I would like to know how to solve this algorithm Help Pls

    
answered by 22.11.2018 в 18:17