d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Forcing A Number In A Random Pw Generator
Add Reply New Topic New Poll
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
Apr 6 2013 07:03am
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
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
Apr 6 2013 07:47am
fixed by using "password=password.substring(0,index)+num+password.substring(index+1,password.length());"
still wondering why the more intuitive way did not work.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 6 2013 09:34am
keep in mind that this is java. strings are not mutable, eg string objects cannot change.

charAt(..) returns a constant char. this isn't an array.

so we'll break it down with an example
password = "password", index = 0, num = 5

password.charAt(index) = num;

password.charAt(0) is 'p'
so you're trying to do:
'p' = 5;

what do you think that's gonna do? since 'p' isn't a variable, it's gonna bitch at you.

what you can do to use this intuitive way is convert the string to an array, change it in the array, and build a new string

i forget what the method name is, but something similar to this
char[] pw = password.toCharArray();
pw[index] = num;
password = new String(pw);

alternatively, you can use something like StringBuilder, which is mutable.

This post was edited by carteblanche on Apr 6 2013 09:37am
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
Apr 6 2013 10:07am
Thank you very much. Still doing my baby steps in java. :)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll