So I’m actually writing a java password generator using ASCII chars and the Math.random() method for my AP Computer Science class. In a nutshell, I’ll have a menu, 1 - 5, in which you can choose for your password to have lowercase letters, numbers, uppercase letters, and punctuation symbols respectively. (Each one has the ones before it as well.) You choose the menu option, choose the number of letters in a password, and then the program generates using a huge nested loop and I’m sure you can imagine it. I am virtually done, but my one problem is the way I’m generating each char. My lowercase letters work fine, I use the code:
for(password = ""; password.length() < lengthPass; password += randomChar) {
randNum = (int)(Math.random() * 25) + 97;
randomChar = (char)randNum;
}
This should give you an idea of how I’m running it. Anyways, this one does work, but in my next menu item, where I use numbers AND lowercase letters, what I’m printing out is not what I want. My code is:
else if (choice == 2) {
for(password = ""; password.length() < lengthPass; password += randomChar) {
randNum = Math.random();
if (randNum < 0.5) {
randNumber = (int)(randNum * 2) * 25 + 97;
randomChar = (char)randNum;
}
else {
randNum = (int)(randNum * 9) + 48;
randomChar = (char)randNum;
}
So this looks pretty similar to the first one, and to me, there is nothing noticeably wrong. However, what my console prints are things such as:
However, when I copy and paste this, I only get “8655”. This isn’t really a big deal, I’m just trying to provide any information I can. Honestly, I don’t know why it’s happening, and I don’t want to know how to fix it, but I would like to know what’s wrong, or even if you can tell what’s wrong. Sorry that this was so long, but I’m really struggling.