Menú

Mostrar Mensajes

Esta sección te permite ver todos los mensajes escritos por este usuario. Ten en cuenta que sólo puedes ver los mensajes escritos en zonas a las que tienes acceso en este momento.

Mostrar Mensajes Menú

Mensajes - pancho992

#1
Hola, soy estudiante y de verdad este trabajo me tiene ya con las meas ojeras xD ... si alguien se apiada de mi ... le estare muy agradecido (Y)

pd : tengo hecho una validador de rut .. y deberia guardarlos en un archivo.txt ... ese es mi drama la vez no cacho como guardar lo que ingreso
una vez que hago click en el boton ...  y me falta poder los otros 2 jtextfield con nombre y apellido los que tambien tiene que ir guardados en el .txt

Profavor ayudenme :) !!

Código: java
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class ValidarRut   extends JFrame implements ActionListener {

    private JButton btnValidar;
    private JTextField txtRut;
    private JLabel labRut;

    public ValidarRut()
    {
    super  ( " Validación de RUT" );
        this.setSize(400, 200);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);       

        // Agregamos elementos a la ventana
        this.setLayout( new FlowLayout(FlowLayout.LEFT, 20, 10)  );

        labRut = new JLabel(" Rut");
        txtRut = new JTextField(30);
        txtRut.setText(" ");
        btnValidar = new JButton("Precione Para Validar");
        btnValidar.addActionListener(this);
        this.add(labRut);
        this.add(txtRut);
        this.add(btnValidar);

        this.setVisible(true);

    }

    public static void main(String[] args) {
        // TODO code application logic here
        new ValidarRut();
    }

    public void actionPerformed(ActionEvent e) {

        // Validamos el RUt
        if (e.getSource()==btnValidar)
        {
            if ( txtRut.getText().length() > 0  )
            {
                // Creamos un arreglo con el rut y el digito verificador
                String[] rut_dv = txtRut.getText().replace(".","").split("-");
                // Las partes del rut (numero y dv) deben tener una longitud positiva
                if ( rut_dv.length == 2   )
                {
                    // Capturamos error (al convertir un string a entero)
                    try
                    {
                        int rut = Integer.parseInt( rut_dv[0] );
                        char dv = rut_dv[1].charAt(0);

                        // Validamos que sea un rut valido según la norma
                        if ( this.ValidarRut(rut, dv)  )
                        {
                            JOptionPane.showMessageDialog(rootPane, "Rut correcto");
                        }
                        else
                        {
                            JOptionPane.showMessageDialog(rootPane, "Rut incorrecto");
                        }
                    }
                    catch( Exception ex )
                    {
                        System.out.println(" Error " + ex.getMessage());
                    }
                }
            }
        }
    }

    /*
     * Método Estático que valida si un rut es válido
     * Fuente : http://www.creations.cl/2009/01/generador-de-rut-y-validador/
     */
    public static boolean ValidarRut(int rut, char dv)
    {
   
        int m = 0, s = 1;
        for (; rut != 0; rut /= 10)
        {
            s = (s + rut % 10 * (9 - m++ % 6)) % 11;
        }
        return dv == (char) (s != 0 ? s + 47 : 75 );
    }

}