0

I am making a random password Generator and want to add an array value to another array "pa" in order to make a single string password. Any help would be appreciated.

package project;
 import java.util.Scanner;

import javax.swing.JOptionPane;

import java.util.Random;
public class RandomPass {

public static void Randpass(){
Scanner scan = new Scanner(System.in);
Random rand = new Random();
String[] alphabet=  {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
JOptionPane.showMessageDialog(null, "Welcome to RandomPassword Generator");
int c= Integer.parseInt(JOptionPane.showInputDialog(null,"How long do you want the password"));
int nc =0-c;
int c2=c/2;
int nc2= 0-c2;
int ncm =(nc+1)/2;
String pa [];
if(c%2==0){
    for(int x=nc2;x<0;x++){
        int alphanum =rand.nextInt(26);
        pa.add(alphabet[alphanum]);
        int numNum =rand.nextInt(10);
        pa.add(numNum);
    }


}else{
    for(int x=ncm;x<0;x++){
        int alphanum =rand.nextInt(26);
        int numNum =rand.nextInt(10);

    }
}

}
}
3
  • 1
    Have you looked up the JavaDoc for Collections and or Arrays? Commented Feb 20, 2016 at 16:36
  • You might want to use an ArrayList. Arrays have a fixed size and work with indexes. Commented Feb 20, 2016 at 16:37
  • Use a StringBuilder. Also, which version of Java is that? Commented Feb 20, 2016 at 16:39

1 Answer 1

1
public class Hello {
private static final String CHAR_LIST = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
private static final int RANDOM_STRING_LENGTH = 10;
    public String generateRandomString(){

        StringBuffer randStr = new StringBuffer();
        for(int i=0; i<RANDOM_STRING_LENGTH; i++){
            int number = getRandomNumber();
            char ch = CHAR_LIST.charAt(number);
            randStr.append(ch);
        }
        return randStr.toString();
    }


    private int getRandomNumber() {
        int randomInt = 0;
        Random randomGenerator = new Random();
        randomInt = randomGenerator.nextInt(CHAR_LIST.length());
        if (randomInt - 1 == -1) {
            return randomInt;
        } else {
            return randomInt - 1;
        }
    }
    public static void main(String[] args) {
        Random rand =  new Random();
        Hello msr = new Hello();
        List<String> list = new ArrayList<String>();
        while(true)
        {
            list.add(msr.generateRandomString());
            //System.out.println("happy birthday smash from "+msr.generateRandomString());
        }
    }

}

I did for my birthday i made some modifications for it hope my code works.Happy coding

Sign up to request clarification or add additional context in comments.

1 Comment

watch iam using infinite loop here loop according to your feasible set

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.