viernes, 30 de mayo de 2014

Contar vocales de un cuadro de texto "preparando examen"


package unidad4u352;


public class Vocales {
        String cadena;
       
      
        int vocales[];

    public Vocales(String cadena) {
   

     this.cadena = cadena;
        vocales = new int[5];
        contarVocales();
    }
       
   public int getA()
   {
    
       return vocales[0];
   }
  
   public int getE()
   {


       //si queremos actualizar podríamos hacer una llamada a:

       //contarVocales();
       return vocales[1];
   }
   
   public int getI()
   {
     
       return vocales[2];
   }
   public int getO()
   {
     
       return vocales[3];
   }
   public int getU()
   {
      
       return vocales[4];
   }
  
   public int [] getVocales(){
       contarVocales();
       return vocales;
   }
  
       
   private void contarVocales(){
        int cont=0,pos;
        String vocal = "aeiou";
      
        for (int i = 0; i < cadena.length(); i++) {
           pos = vocal.indexOf(cadena.toLowerCase().charAt(i));
           if (  pos != -1  ) ++vocales[pos];
        }
   }
  

  
    }
   


// Ventana aplicación visual
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package unidad4u352;

/**
*
* @author Juan
*/
public class VentVocales extends javax.swing.JFrame {

    /**
     * Creates new form VentVocales
     */
    public VentVocales() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        txtCadena = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lblA = new javax.swing.JLabel();
        lblE = new javax.swing.JLabel();
        lblI = new javax.swing.JLabel();
        lblO = new javax.swing.JLabel();
        lblU = new javax.swing.JLabel();
        btnCalcular = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txtCadena.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtCadenaKeyTyped(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtCadenaKeyReleased(evt);
            }
        });

        jLabel1.setText("A:");

        jLabel2.setText("E:");

        jLabel3.setText("I:");

        jLabel4.setText("O:");

        jLabel5.setText("U:");

        lblA.setText("0");

        lblE.setText("0");

        lblI.setText("0");

        lblO.setText("0");

        lblU.setText("0");

        btnCalcular.setText("Contar vocales");
        btnCalcular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCalcularActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(txtCadena, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblA)
                            .addComponent(lblE)
                            .addComponent(lblI)
                            .addComponent(lblO)
                            .addComponent(lblU, javax.swing.GroupLayout.Alignment.LEADING)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(122, 122, 122)
                        .addComponent(btnCalcular, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(99, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(txtCadena, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCalcular)
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblA))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lblE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lblI))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(lblO))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(lblU))
                .addContainerGap(100, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>
    private void contar(){
         int [] numVocales;
        Vocales numTotal;
       
        numTotal=new Vocales(txtCadena.getText());
        lblA.setText(""+numTotal.getA());
        lblE.setText(String.valueOf(numTotal.getE()));
        lblI.setText(""+numTotal.getI());
        lblO.setText(""+numTotal.getO());
        lblU.setText(""+numTotal.getU());
    }
   
    private void btnCalcularActionPerformed(java.awt.event.ActionEvent evt) {
      
        contar();
       
    }

    private void txtCadenaKeyReleased(java.awt.event.KeyEvent evt) {
    contar();        // TODO add your handling code here:
    }

    private void txtCadenaKeyTyped(java.awt.event.KeyEvent evt) {
//       char car = evt.getKeyChar();
//      
//       if(txtCadena.getText().length()>8) evt.consume();
//       else if ((txtCadena.getText().length()<8)&&(car<'0' || car>'9')) evt.consume(); 
//       else if  ((txtCadena.getText().length()==8)&&(car>='0' && car<='9')) evt.consume();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VentVocales.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VentVocales.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VentVocales.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VentVocales.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new VentVocales().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton btnCalcular;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel lblA;
    private javax.swing.JLabel lblE;
    private javax.swing.JLabel lblI;
    private javax.swing.JLabel lblO;
    private javax.swing.JLabel lblU;
    private javax.swing.JTextField txtCadena;
    // End of variables declaration
}