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)

Filburt
  • 17,626
  • 12
  • 64
  • 115
user2965503
  • 13
  • 1
  • 5
  • 1
    Clue: Look at ascii characters 65 onwards... – Software Engineer Jan 20 '14 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. – takendarkk Jan 20 '14 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). – user2965503 Jan 20 '14 at 16:35
  • 1
    +1 for asking for pointers but not final answer. :) – Tim B Jan 20 '14 at 16:35
  • [http://www.asciitable.com/](http://www.asciitable.com/). Just to start you out - 65 = `A`, 66 = `B`, etc... – takendarkk Jan 20 '14 at 16:36
  • @csmckelvey By integer range do you mean the length of the String I'm trying to create or the range of numbers I wish to use ? Length would be 8 (7 numbers plus 1 letter.) and range would be any number at least 7 numbers long. Could you give me the code for turning a int into a char? Also turning an Int into a string is .toString();, right? I'm sorry but my tutor is terrible. Believe it or not I'v had to teach half my class how to code even though I'v only started learning this year... – user2965503 Jan 20 '14 at 16:39
  • @TimB Thanks bud, I'm looking to make a career out of programming eventually and just looking for answers is gonna get me nowhere – user2965503 Jan 20 '14 at 16:40
  • @csmckelvey So 65-90 would be the letters I'm looking for (Upper-case) any websites or anything that could show me how to integrate that into code? I don't wanna be taking up all your time with questions. – user2965503 Jan 20 '14 at 16:42
  • By integer range I mean if you want chars between a-z, generate random number between 97-122. Turning int into char `char myChar = 100` or some other int. For integer into a String `String myString = Integer.valueOf(100)` or some other int. – takendarkk Jan 20 '14 at 16:43
  • You are correct about the int range. – takendarkk Jan 20 '14 at 16:43
  • See my answer for a "cleaner" way to get the range though. Java will convert 'A' to 65 for you. – Tim B Jan 20 '14 at 16:43
  • @TimB I'm just trying to let him know why your way works, like why `a` + 25 is a character. – takendarkk Jan 20 '14 at 16:45
  • @TimB Not sure if I can quote two people into this but this is for csmc aswell. Just wanna say thanks for helping, I'm gonna have a good look at this as its been frying my head for the past few days.. Tim your method is very clear, I'm gonna try and run a sample in my IDE. CSMC with your method, writing char myChar=100 would give the char 'd' right? – user2965503 Jan 20 '14 at 16:50
  • [same question](http://stackoverflow.com/questions/41107/how-to-generate-a-random-alpha-numeric-string) – Ahmad R. Nazemi Oct 09 '16 at 13:08

3 Answers3

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.

Tim B
  • 40,716
  • 16
  • 83
  • 128
  • 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? – user2965503 Jan 20 '14 at 16:46
  • 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. – Tim B Jan 20 '14 at 16:52
  • 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. – user2965503 Jan 20 '14 at 16:58
  • 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. – user2965503 Jan 20 '14 at 17:01
  • I'd upvote you for the help (Greatly appreciated) but I need another 7 rep to do so sadly – user2965503 Jan 20 '14 at 17:05
  • Don't worry, you will get rep soon enough if you ask/answer a few good questions. :) I would have a single method to return the string on its own, then another method to create an array full of X strings. Each method should do one thing, and do it well, and be reusable. – Tim B Jan 20 '14 at 17:58
  • What I ended up doing was declaring the array of 50 in the main. I then created a method to generate PPS numbers and passed the empty array in, when rng runs it fills the array. I then have another method which uses to PPSN numbers as a password. They enter their PPS and the list of PPS numbers is searched if a match is found the user is let in :) – user2965503 Jan 22 '14 at 12:13
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

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
  • 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 – user2965503 Jan 20 '14 at 16:45
  • 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? – user2965503 Jan 20 '14 at 17:19
  • @user2965503 Read http://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextInt(int) – Rakesh KR Jan 20 '14 at 17:24
  • @RakeshKR Thanks, I was having trouble finding a source for the info :) – user2965503 Jan 20 '14 at 17:26
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

Felipe T.
  • 637
  • 1
  • 8
  • 14