Hallo,
ich habe selbstständig versucht einen Passwort-Generator zu coden. Soweit hat auch alles funktioniert, nur zum Ende möchte ich alle Strings (Sonderzeichen, Zahlen, Buchstaben) nochmal mischen. Da die Strings aber in den if-clauses sind weiß ich nicht mehr weiter. Kann mir vielleicht jemand helfen?
[CODE lang="java" highlight="89-165"]import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;
import java.util.Random;
public class Generator extends JFrame {
private JPanel contentPane;
private JTextField anzahlZeichen;
private JTextField pw;
String eingabe;
int value;
public static void main(String[] args) {
Generator frame = new Generator();
frame.setVisible(true);
}
public Generator() {
setTitle("Generator");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(1100, 600, 450, 308);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
ImageIcon icon = new ImageIcon("C:/Users/Simon/IdeaProjects/generator/src/th.jpg");
icon.setImage(icon.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
JLabel l1 = new JLabel(icon);
add(l1);
anzahlZeichen = new JTextField();
contentPane.add(anzahlZeichen, "cell 0 1,growx,aligny top");
anzahlZeichen.setColumns(5);
pw = new JTextField();
contentPane.add(pw);
pw.setColumns(15);
JLabel T = new JLabel("Anzahl Zeichen");
contentPane.add(T, "cell 0 0,growx,aligny top");
JCheckBox checkBoxBuchstaben = new JCheckBox("Buchstaben");
JCheckBox checkBoxZahlen = new JCheckBox("Zahlen");
JCheckBox checkBoxSonderzeichen = new JCheckBox("Sonderzeichen");
contentPane.add(checkBoxBuchstaben);
contentPane.add(checkBoxSonderzeichen);
contentPane.add(checkBoxZahlen);
JButton create = new JButton("Create");
contentPane.add(create);
create.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (anzahlZeichen.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "Keine Zahl angegeben!");
} else {
eingabe = anzahlZeichen.getText();
value = Integer.parseInt(eingabe);
}
if(!checkBoxBuchstaben.isSelected() && !checkBoxZahlen.isSelected() && !checkBoxSonderzeichen.isSelected() ){
JOptionPane.showMessageDialog(null, "Nichts ausgewählt");
}
for(int i = 1; i <= value; i++) {
if (checkBoxBuchstaben.isSelected()) {
String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String randomString = "";
int numbers = (int) (Math.random()* 10);
int length = 2;
Random rand = new Random();
char[] text = new char[length];
for(int x = 1; x < length; x++){
text[x] = chars.charAt(rand.nextInt(chars.length()));
}
for(int x = 1; x < text.length; x++) {
randomString += text[x];
}
System.out.println(randomString);
String lower = randomString.toLowerCase(Locale.ROOT);
System.out.println(lower);
}
if (checkBoxSonderzeichen.isSelected()) {
String chars = "!@#&()–[{}]:;',?/*\"~$^+=<>";
String randomString = "";
int numbers = (int) (Math.random()* 10);
int length = 2;
Random rand = new Random();
char[] text = new char[length];
for(int x = 1; x < length; x++){
text[x] = chars.charAt(rand.nextInt(chars.length()));
}
for(int x = 1; x < text.length; x++) {
randomString += text[x];
}
System.out.println(randomString);
}
if (checkBoxZahlen.isSelected()) {
String chars = "1234567890";
String randomString = "";
int numbers = (int) (Math.random() * 10);
int length = 2;
Random rand = new Random();
char[] text = new char[length];
for (int x = 1; x < length; x++) {
text[x] = chars.charAt(rand.nextInt(chars.length()));
}
for (int x = 1; x < text.length; x++) {
randomString += text[x];
}
}
}
}
});
}
}
[/CODE]
ich habe selbstständig versucht einen Passwort-Generator zu coden. Soweit hat auch alles funktioniert, nur zum Ende möchte ich alle Strings (Sonderzeichen, Zahlen, Buchstaben) nochmal mischen. Da die Strings aber in den if-clauses sind weiß ich nicht mehr weiter. Kann mir vielleicht jemand helfen?
[CODE lang="java" highlight="89-165"]import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;
import java.util.Random;
public class Generator extends JFrame {
private JPanel contentPane;
private JTextField anzahlZeichen;
private JTextField pw;
String eingabe;
int value;
public static void main(String[] args) {
Generator frame = new Generator();
frame.setVisible(true);
}
public Generator() {
setTitle("Generator");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(1100, 600, 450, 308);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
ImageIcon icon = new ImageIcon("C:/Users/Simon/IdeaProjects/generator/src/th.jpg");
icon.setImage(icon.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
JLabel l1 = new JLabel(icon);
add(l1);
anzahlZeichen = new JTextField();
contentPane.add(anzahlZeichen, "cell 0 1,growx,aligny top");
anzahlZeichen.setColumns(5);
pw = new JTextField();
contentPane.add(pw);
pw.setColumns(15);
JLabel T = new JLabel("Anzahl Zeichen");
contentPane.add(T, "cell 0 0,growx,aligny top");
JCheckBox checkBoxBuchstaben = new JCheckBox("Buchstaben");
JCheckBox checkBoxZahlen = new JCheckBox("Zahlen");
JCheckBox checkBoxSonderzeichen = new JCheckBox("Sonderzeichen");
contentPane.add(checkBoxBuchstaben);
contentPane.add(checkBoxSonderzeichen);
contentPane.add(checkBoxZahlen);
JButton create = new JButton("Create");
contentPane.add(create);
create.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (anzahlZeichen.getText().isEmpty()) {
JOptionPane.showMessageDialog(null, "Keine Zahl angegeben!");
} else {
eingabe = anzahlZeichen.getText();
value = Integer.parseInt(eingabe);
}
if(!checkBoxBuchstaben.isSelected() && !checkBoxZahlen.isSelected() && !checkBoxSonderzeichen.isSelected() ){
JOptionPane.showMessageDialog(null, "Nichts ausgewählt");
}
for(int i = 1; i <= value; i++) {
if (checkBoxBuchstaben.isSelected()) {
String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String randomString = "";
int numbers = (int) (Math.random()* 10);
int length = 2;
Random rand = new Random();
char[] text = new char[length];
for(int x = 1; x < length; x++){
text[x] = chars.charAt(rand.nextInt(chars.length()));
}
for(int x = 1; x < text.length; x++) {
randomString += text[x];
}
System.out.println(randomString);
String lower = randomString.toLowerCase(Locale.ROOT);
System.out.println(lower);
}
if (checkBoxSonderzeichen.isSelected()) {
String chars = "!@#&()–[{}]:;',?/*\"~$^+=<>";
String randomString = "";
int numbers = (int) (Math.random()* 10);
int length = 2;
Random rand = new Random();
char[] text = new char[length];
for(int x = 1; x < length; x++){
text[x] = chars.charAt(rand.nextInt(chars.length()));
}
for(int x = 1; x < text.length; x++) {
randomString += text[x];
}
System.out.println(randomString);
}
if (checkBoxZahlen.isSelected()) {
String chars = "1234567890";
String randomString = "";
int numbers = (int) (Math.random() * 10);
int length = 2;
Random rand = new Random();
char[] text = new char[length];
for (int x = 1; x < length; x++) {
text[x] = chars.charAt(rand.nextInt(chars.length()));
}
for (int x = 1; x < text.length; x++) {
randomString += text[x];
}
}
}
}
});
}
}
[/CODE]