Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java error in password generator program

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Angry Java error in password generator program

    For some reason my numbers aren't getting into my password ever. Please help.

    import java.util.Random;
    import java.util.*;
     
    public class PasswordGenerator
    {
     
     public static void main ( String [] args )
     {
     
      int len, k, rn, su;
      char ch;
      String password = "";
     
      len = (int) (Math.random() * 3 ) ;
     
      for (k = 1; k <=8 ; k++) 
      {
     
       rn = (int) ( 0 + Math.random() * 26 );
       su = (int) ( 1 + Math.random() * 2);
     
       if (su == 1)
     
        ch = (char) (97 + rn);
     
       else
     
        ch = (char) (65 + rn);
        password += ch;
      }
     
      while (k <= len) 
      {
     
       rn = (int) (0 + Math.random() * 10 ); 
       password += rn;
       k++;
     
      }
      System.out.println (password);
     
     }
    }
    Last edited by Deep_4; November 7th, 2012 at 10:52 PM.


  2. #2
    Little Star
    Join Date
    May 2009
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 7 Posts

    Default Re: What am I missing in my code?

    for (k = 1; k <=8 ; k++) 
      {
     
       rn = (int) ( 0 + Math.random() * 26 );
       su = (int) ( 1 + Math.random() * 2);
     
       if (su == 1)
     
        ch = (char) (97 + rn);
     
       else
     
        ch = (char) (65 + rn);
        password += ch;
      }

    When your for loop ends variable k will have value of 9

      while (k <= len) 
      {
     
       rn = (int) (0 + Math.random() * 10 ); 
       password += rn;
       k++;
     
      }

    Then you run while loop only if k <= len (k is 9 and len is never bigger than 2)
    That is why you never enter inside while loop.

    Try this instead:

    public static void main ( String [] args )
    	 {
     
    	  int len, k, rn, su;
    	  char ch;
    	  String password = "";
     
    	  len = (int) (Math.random() * 3 ) ;
     
    	  for (k = 1; k <=8 ; k++) 
    	  {
     
    	   rn = (int) ( 0 + Math.random() * 26 );
    	   su = (int) ( 1 + Math.random() * 2);
     
    	   if (su == 1)
     
    	    ch = (char) (97 + rn);
     
    	   else
     
    	    ch = (char) (65 + rn);
    	    password += ch;
    	  }
     
    	  while (k >= len) 
    	  {
     
    	   rn = (int) (0 + Math.random() * 10 ); 
    	   password += rn;
    	   k--;
     
    	  }
    	  System.out.println (password);
    	 }

Similar Threads

  1. [SOLVED] Java program to generate 10 random integers and then sum computed
    By Lizard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 14th, 2009, 12:33 PM
  2. Replies: 2
    Last Post: March 6th, 2009, 03:00 PM