stringa troppo corta

Stato
Discussione chiusa ad ulteriori risposte.

kr1pn0$

Utente Silver
14 Ottobre 2008
69
5
0
70
ciao ragazzi,
aiuto...
fino a poco tempo fa andava...
questo programma:
Codice:
public class password_gen extends javax.swing.JFrame {
    /** Creates new form password_gen */
    // creates a public alfabeto
    String alfabeto = "qwertyuiopasdfghjklzxcvbnm|!£$%&/()=?^1234567890ìé*òàù,.-;:_";
    int n=0; // public lenght password
    public password_gen() {
        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() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jToggleButton1 = new javax.swing.JToggleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Password generation by kr1pn0$");
        setResizable(false);

        jButton1.setText("calcola");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("altro...");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setText("by Kr1pn0$, 2008-2009");

        jTextField1.setEditable(false);

        jLabel2.setText("La lunghezza predefinita è di 16 livello normale e 32 professionale");

        jToggleButton1.setText("professionale");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(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()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToggleButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 337, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(47, 47, 47)
                                        .addComponent(jLabel1)))
                                .addGap(18, 18, 18)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
                            .addComponent(jLabel2))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToggleButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        n=16;
        String password ="";
        for(int i=0; i<n; ++i)
        {
            int temp=(int)(Math.random()*40); //creo un indice casuale
            password = alfabeto.substring(temp, temp+1); //prendo un carattere
        }
        jTextField1.setText(password);
    }

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        n=32;
    }

    /* jTextField1
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new password_gen().setVisible(true);

            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JToggleButton jToggleButton1;
    // End of variables declaration

}
poi non so che ho fatto,invece di uscire una stringa casuale di 16 caratteri mi esce una lettera casuale...
come mai???

essendo che è tanto code, vi iporto il meccanismo di generazione raNdom:
Codice:
n=16;
        String password ="";
        for(int i=0; i<n; ++i)
        {
            int temp=(int)(Math.random()*40); //creo un indice casuale
            password = alfabeto.substring(temp, temp+1); //prendo un carattere
        }
        jTextField1.setText(password);
 
Beh
Codice:
 password = alfabeto.substring(temp, temp+1); //prendo un carattere
dovrebbe essere sostituito con
Codice:
 password += alfabeto.substring(temp, temp+1); //prendo un carattere
 
precisamente stoner,la differenza sta nell'assegnazione alla variabile,se tu fai:
Codice:
password = alfabeto.substring(temp, temp+1);
assegni ad ogni ciclo del for il carattere generato alla varibile password,mentre a te serve concatenare di volta in volta il carattere nella variabile.Come giustamente stoner ha detto devi fare:
Codice:
password += alfabeto.substring(temp, temp+1);
è la stessa cosa di:
Codice:
password = password+alfabeto.substring(temp, temp+1);
comunque io solitamente uso charAt(int x) invece di substring(),substring() viene solitamente utilizzato per prendere più di un carattere,mentre charAt() preleva un solo carattere,infatti potresti fare semplicemente:
Codice:
password+=""+alfabeto.charAt(temp);
il ""+ deve essere per forza utilizzato semplicemente perchè charAt() non ritorna una stringa ma un char,tu utilizzando ""+ lo concateni ad una stringa nulla (appunto "") e il problema si risolve :D!
 
ok,
comunque ho creato un programma completo(circa XD) usando un algoritmo leggermente diverso...
Prende con un getValue() il valore della lunghezza da un JSpinner,
e poi come hai detto te:
charAt()
e poi lo scrive come un textfield only reader
 
Stato
Discussione chiusa ad ulteriori risposte.