Results 1 to 10 of 10
  1. #1
    bl00dr3d is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Post [SOLVED]Generate a random code 4 letters long

    Hi everyone, I need to generate a 4 letters long code with unique letters for a program of mine. Heres what I have done till now..

    Java Code:
    private String generateLetter() {
    
    				int n = (int)(Math.random() * 6 + 1);
    				if (n == 1); {
    					String letter = A;
    				}
    				if (n == 2); {
    					String letter = B;
    				}
    				if (n == 3); {
    					String letter = C;
    				}
    				if (n == 4); {
    					String letter = D;
    				}
    				if (n == 5); {
    					String letter = E;
    				}
    				if (n == 6); {
    					String letter = F;
    				}
    				return letter;
    			}
    
    
    	private String generateSecretCode() {
    
    
    		String firstLetter = generateLetter();
    		String secondLetter = generateLetter();
    		String thirdLetter = generateLetter();
    		String fourthLetter = generateLetter();
    
    		String secretCode = firstLetter + secondLetter + thirdLetter + fourthLetter;
    
    		return secretCode;
    	}
    
    When I try to compile this I get errors like - 
    
    C:\A1Stage5\MyProgram.java:11: cannot find symbol
    symbol  : variable A
    location: class MyProgram
    					String letter = A;
    I would really appreciate it if any of you could point out the mistake that I am making and advise me on what I should do.

    Thanks.
    Last edited by bl00dr3d; 04-06-2009 at 03:17 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Every variable (String here) that you declare within an if block is visible only within that if block, and is invisible elsewhere in your method. Perhaps it would be best to declare a single String variable letter early in your method and before all the if blocks.

    Also, this will never work:
    Java Code:
    String fu = A;
    Far better would be
    Java Code:
    String fu = "A";

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Myself, I'd do it a little differently though. I'd have generateLetter accept a String parameter, say called letters that was a String that held all the possible that could be used to generate a random letter. For instance in your case, you'd pass "ABCDEF" as a parameter.

    I'd then use a Random object to generate a random number between 0 and the length of the letters parameter. This index could then be used to get a random char from the letters String via letters.charAt(index), then return this char, or if desired change it to a String before returning it with String.valueOf(...).

  4. #4
    bl00dr3d is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Oh my god! how could I forget about that! lol Thanks for pointing that out.
    I read the way you said you would do it and it makes sense to do it that way but the thing is that the code is supposed to be generated automatically by the computer so maybe using an array would be a good idea?
    This is the code that I wrote using an array but it keeps giving me AAAA as the code :(

    Java Code:
    private String generateSecretCode() {
    		String[] letters = {"A", "B", "C", "D", "E", "F"};
    		String firstLetter = letters[(int)Math.random() * 6];
    		String secondLetter = letters[(int)Math.random() * 6];
    		String thirdLetter = letters[(int)Math.random() * 6];
    		String fourthLetter = letters[(int)Math.random() * 6];
    
    		String secretCode = firstLetter + secondLetter + thirdLetter + fourthLetter;
    		return secretCode;
    	}
    I cant seem to understand why it would give me A as the only letter again and again..
    Last edited by bl00dr3d; 04-06-2009 at 05:19 AM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    What's the difference between this:
    Java Code:
    (int)Math.random() * 6
    and this:
    Java Code:
    (int)(Math.random() * 6)
    ?

  6. #6
    bl00dr3d is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    What's the difference between this:
    Java Code:
    (int)Math.random() * 6
    and this:
    Java Code:
    (int)(Math.random() * 6)
    ?
    ohhh without brackets the (int) gets priority over Math.random * 6. That makes sense.
    While I was waiting for your reply, I tried doing it your way and everything seems to be fine and dandy except for this one error that I keep getting..

    Java Code:
    private String generateSecretCode() {
    		String validLetters = "ABCDEF";
    		char firstLetter = validLetters.charAt((int)(Math.random() * 6));
    		char secondLetter = validLetters.charAt((int)(Math.random() * 6));
    		char thirdLetter = validLetters.charAt((int)(Math.random() * 6));
    		char fourthLetter = validLetters.charAt((int)(Math.random() * 6));
    
    		String secretCode = firstLetter + secondLetter + thirdLetter + fourthLetter;
    		return secretCode;
    	}
    
    C:\A1Stage5\MyProgram.java:14: incompatible types
    found   : int
    required: java.lang.String
    		String secretCode = firstLetter + secondLetter + thirdLetter + fourthLetter;
    Last edited by bl00dr3d; 04-06-2009 at 04:59 AM.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    you could build your String with a StringBuilder

  8. #8
    bl00dr3d is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    StringBuilder? I dont think I am familiar with that term but I will look it up. Though for now my code is working so thanks a lot for helping out :D

    One more question. I want all of the four letters to be unique. I looked around on google and people have used the for loop which is something that I dont understand very well right now so is there any other way to ensure that the four letters are unique? maybe by using if or while loops?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I want all of the four letters to be unique.
    Holy creeping requirements Batman!

    Seriously though it would be best if you gave all requirements in the first post as this changes things a bit. In this situation I'd use a List here and use Collections.shuffle(myList). But it's getting late for me. Best of luck.

  10. #10
    bl00dr3d is offline Member
    Join Date
    Apr 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Holy creeping requirements Batman!
    Haha sorry about that and thanks for all your help man. I really appreciate it :D

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  2. Trying to Generate Random number
    By PeterFeng in forum New To Java
    Replies: 10
    Last Post: 01-14-2009, 08:37 AM
  3. Letter with Letters
    By elgatoboricua in forum New To Java
    Replies: 7
    Last Post: 09-16-2008, 02:59 PM
  4. Generate a random number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:23 AM
  5. How to generate random number in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 07:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •