-2

I wrote a program where I want some output which starts with an uppercase letter + digit. Then some random generation from the allowed characters.

But I find the output contains spaces like: k r w ea. Whereas I expected : W3krwea.

I don't know why these spaces appear in my output.

import java.util.Random;
import sun.security.util.Password;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author taskin
 */

    public class RPG
{

    public static String generate(String allowedCharacters,
                                  boolean mustHaveUppercase, boolean mustHaveDigit,
                                  int minLength, int maxLength)
    {
        Random rand=new Random();
        String rp = null; // Random Password
        int Length=allowedCharacters.length();
        char[] password=new char[25];
        int [] asc=new int[18];
        char[] UpperCase=new char[25];
        char[] Digit=new char[25];
        char[] Mixed=new char[25];
        int point1=0,point2=0,point3=0;

        for (int i=0;i<Length;i++)
        {
            asc[i]=(int)allowedCharacters.charAt(i);

        }
        for (int k=0;k<Length;k++)
        {
            if (asc[k]>=65 && asc[k]<=90)
            {
                UpperCase[point1]=(char)asc[k];
                point1++;
                continue;
            }
            else if (asc[k]>=48 && asc[k]<=57)
            {
                Digit[point2]=(char)asc[k];
                point2++;
                continue;
            }
            else
            {
                Mixed[point3]=(char)asc[k];
                point3++;
            }

        }
        StringBuilder strbld=new StringBuilder();
        int length=maxLength-minLength+1+minLength;
        strbld.append(UpperCase[rand.nextInt(UpperCase.length)]);//get a uppercase char
        strbld.append(Digit[rand.nextInt(Digit.length)]);//get a digit
        for(int i=0;i<length-2;i++){
            strbld.append(Mixed[rand.nextInt(Mixed.length)]);
        }
        rp=strbld.toString();

        // Your code to set rp appropriately goes here

        return rp;
    }

    public static void main(String[] args)
    {
        String allowedCharacters = "weakPasSWorD1234$*";
        boolean mustHaveUppercase = true;
        boolean mustHaveDigit = true;
        int minLength = 3;
        int maxLength = 20;
        String randomPassword = generate(allowedCharacters,
                                         mustHaveUppercase, mustHaveDigit, minLength,
                                         maxLength);
        System.out.println("Random password: " + randomPassword);
    }
}
Leigh
  • 28,765
  • 10
  • 55
  • 103
Taskin
  • 61
  • 1
  • 2
  • 9

1 Answers1

1

The .length what you are using, will return the total length of your Char array.Which is around 25 or something.But,in reality,your length is about 4-5 chars.So,it will give rise to some white spaces.Note: the function is not same as .length() (you misunderstood i think. )

Please look at THIS thread to make the concept clear.

Use the same length what you have used in your Loops. Change the StringBuilder block to :

    StringBuilder strbld=new StringBuilder();
    int length=maxLength-minLength+1+minLength;
    System.out.println("n     "+strbld.append(UpperCase[rand.nextInt(point1)]));//;

    //get a uppercase char
    strbld.append(Digit[rand.nextInt(point2)]);
    for(int i=0;i<length-2;i++){
        strbld.append(Mixed[rand.nextInt(point3)]);

Output:

  W1aearere$karasa$akke

No white space.

Community
  • 1
  • 1
joey rohan
  • 3,505
  • 5
  • 33
  • 70
  • @Taskin welcome again! Well next time any problem with this password thing, just ask me only.Too many questions on same topic are not worth while.(just like your last 2 threads)Take care! – joey rohan Jan 22 '13 at 18:38