Hey. I'm trying to force a number within a randomly generated password.
The idea is to check whether any character of the password is a either 0,1,2,3,4,5,6,7,8 or 9. The boolean hasnum will be set accordingly.
If hasnum is false, I want to replace a character at a random index with a randomly generated number between 0 and 9.
It all goes well until the line "password.charAt(index)=num;". The compiler quits with an unexpected type error: required variable, found value.
Here's the code:
Code
import java.util.Random;
import java.util.Scanner;
class twenty4B
{
public static void main (String[] args)
{
Scanner in=new Scanner(System.in);
Random rand=new Random();
String pool, password="";
int digits=0,counter=1, index;
boolean hasnum=false;
char num;
pool="abcdefghijklmnopqrstuvwxyz";
pool=pool+pool.toUpperCase();
pool=pool+"0123456789";
while(digits<5)
{
System.out.println("How many digits for your pw? (minimum: 5)");
digits=in.nextInt();
}
while(counter<=digits)
{
index=rand.nextInt(pool.length());
password=password+pool.charAt(index);
counter=counter+1;
}
counter=0;
while(counter < password.length() && hasnum==false)
{
if(password.charAt(counter)=='0' || password.charAt(counter)=='1' ||
password.charAt(counter)=='2' || password.charAt(counter)=='3' ||
password.charAt(counter)=='4' || password.charAt(counter)=='5' ||
password.charAt(counter)=='6' || password.charAt(counter)=='7' ||
password.charAt(counter)=='8' || password.charAt(counter)=='9' )
{
hasnum=true;
}
counter=counter=1;
}
if(hasnum==false)
{
index=rand.nextInt(password.length());
num="0123456789".charAt(rand.nextInt(10));
password.charAt(index)=num;
}
System.out.println();
System.out.println("your pw: "+password);
}
}
This post was edited by tt_toby on Apr 6 2013 07:08am