Results 1 to 10 of 10
- 04-06-2009, 03:52 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
[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..
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.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;
Thanks.Last edited by bl00dr3d; 04-06-2009 at 03:17 PM.
-
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:
Far better would beJava Code:String fu = A;
Java Code:String fu = "A";
-
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(...).
- 04-06-2009, 04:35 AM #4
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
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 :(
I cant seem to understand why it would give me A as the only letter again and again..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; }Last edited by bl00dr3d; 04-06-2009 at 05:19 AM.
-
What's the difference between this:
and this:Java Code:(int)Math.random() * 6
?Java Code:(int)(Math.random() * 6)
- 04-06-2009, 04:53 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
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.
-
you could build your String with a StringBuilder
- 04-06-2009, 05:18 AM #8
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
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?
-
Holy creeping requirements Batman!I want all of the four letters to be unique.
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.
- 04-06-2009, 05:32 AM #10
Member
- Join Date
- Apr 2009
- Posts
- 17
- Rep Power
- 0
Similar Threads
-
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Trying to Generate Random number
By PeterFeng in forum New To JavaReplies: 10Last Post: 01-14-2009, 08:37 AM -
Letter with Letters
By elgatoboricua in forum New To JavaReplies: 7Last Post: 09-16-2008, 02:59 PM -
Generate a random number
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:23 AM -
How to generate random number in java
By fernando in forum New To JavaReplies: 1Last Post: 08-01-2007, 07:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks