1

What I'm trying to do is to generate a random string of numbers E.G 2645237 and one char in a string in the range of A-Z E.G. W and combine the two strings to make 2645237W. I can generate a random number no problem. What I'm stuck on is: 1. Generating a random Char as a string. 2. Combining the two strings to create one string. To be clear what it's for is a school assignment to achieve some extra credit in my marking. Like always I'm not looking for the full answer. Some pseudo-code or a working example would be fine but I'd like the final "A-HA!" moment to be my own doing. A final parameter. This end result (the one string) would need to be a generated 50 times differently (I can do this) and then used as a sort of password. (Meant to replicate a PPS number, the added char is the bit that has my whole class stumped).

I'm not looking to cheat my way to a coded answer, just stuck on this problem (We've all been there)

14
  • 1
    Clue: Look at ascii characters 65 onwards... Commented Jan 20, 2014 at 16:29
  • The char data type is actually represented by an integer. Find the integer range of the characters you want, generate a random int in that range, convert it to a char. From there turning an int and a char into strings and combining them should be easy. Commented Jan 20, 2014 at 16:32
  • Hey Engineer, Could you point me in the right direction to where I can learn ascii chars, I'v only been learning for about 3 months in a slow paced class with a below average tutor (Shows his work and expects us to learn just from the raw code). Commented Jan 20, 2014 at 16:35
  • 1
    +1 for asking for pointers but not final answer. :) Commented Jan 20, 2014 at 16:35
  • http://www.asciitable.com/. Just to start you out - 65 = A, 66 = B, etc... Commented Jan 20, 2014 at 16:36

3 Answers 3

2

You can generate a random character simply by doing 'a' (or 'A' for upper case) and then generating a random number from 0 to 25 and adding that to it. i.e. 'a'+3 is 'd'. Note the use of a single quote character to say this is a char literal as opposed to the double quote for a String literal.

That random character can then be appended to the string. StringBuilder would do it for you easily, I'm not sure off hand what the String + operator will do with it.

Sign up to request clarification or add additional context in comments.

7 Comments

So say for example I put in this pseudo-code (turned into proper code in a IDE naturally).. String letter = 'A'+(RNG); Example: String letter = 'A'+(14); Would the output become another letter? The output on this example would be O, right?
Yes. Char addition changes the value of the char, exactly the same as int 65+14 gives 79, 'A'+14 gives letter 79. This is different from + in String which concatenates strings.
Okay I used your method for the char ('A'+3). Then I made a 7 digit number (Hardcoded for now to save time) And combined them using Rakesh KR's method (myChar+""+Num) as this was a serious problem for me previously. Might I also ask one more thing if its not too much trouble ? I'll put it into a seperate comment.
I make the String 4534343W and put the code in a loop (Creating 50 strings and printing them to the bottom. I need the 50 numbers to be in an array. Do I create the array in the same method as the random number generator or do I declare it in the Main? Keeping in mind I will need to call the array into another method allowing a user to check if there code matches one of the 50.
I'd upvote you for the help (Greatly appreciated) but I need another 7 rep to do so sadly
|
0

Try,

Random rn        = new Random();
int    range     = 9999999 - 1000000 + 1;  
int    randomNum =  rn.nextInt(range) + 1000000;  // For 7 digit number
System.out.println(randomNum);

Random rc = new Random();
char   c  = (char)(rc.nextInt(26) + 'A');
System.out.println(c);

String str = randomNum+""+c;        
System.out.println(str);

str prints like 1757217Y

4 Comments

Okay that actually makes me slightly mad, not that you gave me the code (A working example is great!) But I had something very similar to that code yet I could not combine the two variables into the one String. Error said char cannot be dereferenced
Hey Rakesh could you explain how this line in your code works? (char)(rc.nextInt(26) + 'A'); I understand that rc.nextInt(26) adds a value 1-25 to 'A' to give a new letter but how does rc.nextInt() work? I'm gonna guess that it creates a random int for the next int in the range of 26?
@RakeshKR Thanks, I was having trouble finding a source for the info :)
0

To generate the letter and append on your number sequence:

String msg1 = "random number sequence";
Random gen = new Random();
char c = (char) (65 + gen.nextInt(26));
StringBuilder sb = new StringBuilder();
sb.append(msg1);
sb.append(c);
String result = sb.toString();
System.out.println(result);

By the way, 65 is the ascii code of the letter 'A' and gen.nextInt(26) generates a number between 0 and 25, ie, we have a range between 65 and 90 which are the letters' A'-'Z' in ascii table

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.