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 9 of 9

Thread: Random 4 Char Generator Generates 3 Numbers Instead?

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    27
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Random 4 Char Generator Generates 3 Numbers Instead?

    Hi there everyone,

    As you will be able to gather from this code, I am a total beginner. I'm trying to make the basis of a complex (pseudo)random password generator, but instead of 4 chars it is printing a 3 digit number.

    Any help would be greatly appreciated! Thanks



    public class PasswordGen {
     
    	public static void main (String[] args) {
     
    		char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
    		int w = (int) ((Math.random() * 25 +1)); // pick a letter of the alphabet
    		int x = (int) ((Math.random() * 25 +1));
    		int y = (int) ((Math.random() * 25 +1));
    		int z = (int) ((Math.random() * 25 +1));
     
    		char place1 = (alphabet[w]); // assign letter of the alphabet to place in string below
    		char place2 = (alphabet[x]);
    		char place3 = (alphabet[y]);
    		char place4 = (alphabet[z]);
     
    		System.out.println(place1 + place2 + place3 + place4);
    	}
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Random 4 Char Generator Generates 3 Numbers Instead?

    Welcome to the Forum! Thanks for taking the time to learn to post code correctly, and if you haven't already, please read this topic to see other useful info for newcomers.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random 4 Char Generator Generates 3 Numbers Instead?

    instead of 4 chars it is printing a 3 digit number.
    I see code to print 4 chars,
    Which one of the 4 char is not printing? Post the program's output.

    Add some more print statements(1 per variable) to see which one is the problem.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Jul 2014
    Posts
    27
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Random 4 Char Generator Generates 3 Numbers Instead?

    Okay, so changing the string from 'place1 + place2 + place3 + place4' to 'place1', etc, gave me: wnxwknxwknxwknxwknxwknxwknxwknxwnxwknx
    At 'place1 + place2 + place3 + place4' it gives me a number around 433 every time.

    EDIT: Sorry, the long string came from a for loop I forgot to remove. Actual output was a single char.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random 4 Char Generator Generates 3 Numbers Instead?

    char are numeric variables that can be added together with the + operator
    For example what would you expect from this expression: 1+2+3+4?
    1234
    or 10
    The same with 'a'+'b'+'c'+'d'
    it gives a sum not "abcd"

    The + operator is used two ways:
    sum of numeric operands
    concatenation of String objects.

    To get concatenation, use Strings.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    BenjaminJ (July 10th, 2014)

  7. #6
    Junior Member
    Join Date
    Jul 2014
    Posts
    27
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Random 4 Char Generator Generates 3 Numbers Instead?

    I thought I just replied to this, but my post disappeared. Thanks for your concise reply - I have another question; when I put the random number generator in a for loop, it returns the same result over and over again (433, 433, 433) instead of a new calculation every time (433, 564, 794, etc). Is there a way to force it to generate again each time it loops?

    Thanks!

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Random 4 Char Generator Generates 3 Numbers Instead?

    a way to force it to generate again each time it loops?
    Please post the code you are asking about.
    If you define the generator one time and then call its methods repeatedly inside the loop, you should get a new number each time from the method.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #8
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Random 4 Char Generator Generates 3 Numbers Instead?

    Quote Originally Posted by BenjaminJ View Post
    I thought I just replied to this, but my post disappeared. Thanks for your concise reply - I have another question; when I put the random number generator in a for loop, it returns the same result over and over again (433, 433, 433) instead of a new calculation every time (433, 564, 794, etc). Is there a way to force it to generate again each time it loops?

    Thanks!
    Show your code and we can help you.

  10. #9
    Junior Member
    Join Date
    Jul 2014
    Posts
    27
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Random 4 Char Generator Generates 3 Numbers Instead?

    I just realised the answer myself - the only part looping was the print method, not the random number generator. Everything's solved, thanks guys.

Similar Threads

  1. Random Number Generator
    By Rugby_Thompson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2013, 12:58 AM
  2. Random Number Generator Always gives 0
    By tlckl3m3elmo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2012, 03:09 PM
  3. Random math problem generator?
    By CamCompetes in forum Member Introductions
    Replies: 1
    Last Post: March 9th, 2012, 02:42 PM
  4. random number generator
    By java3 in forum Loops & Control Statements
    Replies: 4
    Last Post: February 21st, 2011, 12:00 PM
  5. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM