Note that there are some explanatory texts on larger screens.

plurals
  1. PORandom password generation
    text
    copied!<p>I wrote a program where I want some output which starts with an uppercase letter + digit. Then some random generation from the allowed characters.</p> <p>But I find the output contains spaces like: <code>k r w ea</code>. Whereas I expected : <code>W3krwea</code>.</p> <p>I don't know why these spaces appear in my output.</p> <pre><code>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&lt;Length;i++) { asc[i]=(int)allowedCharacters.charAt(i); } for (int k=0;k&lt;Length;k++) { if (asc[k]&gt;=65 &amp;&amp; asc[k]&lt;=90) { UpperCase[point1]=(char)asc[k]; point1++; continue; } else if (asc[k]&gt;=48 &amp;&amp; asc[k]&lt;=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&lt;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); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload