Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java generating password

 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm trying to create a password generator, this is part of my code. This part of the code basically say that if the user pick "1", the generator would generate a password base on the user input of length "passLength", now for the next part, I need to put lowercase and uppercase random letter for option "2". Uppercase letter are from 65 to 90 ( in ASCII Code) and lowercase are from 97 to 122. How do I make it so the random method generate both ASCII from 65-90 and from 97 to 122? Do I just put int randomLower = (int) ((Math.random()*(25)+(97)) && ((Math.random()*(25)+(65)))? << I try this but it don't seem to be compiling. Thanks



***If my question is confusing, what I am trying to ask is how do I make Java generate random ASCII of only lowercase and upper case letter ( A-Z and a-z)
 
Kathleen Angeles
Ranch Hand
Posts: 123
Firefox Browser Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it don't seem to be compiling.


What is the compilation error message you got?
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea how to make the code to generate both lowercase and uppcase letter. Instead of putting int randomLower = (int)((Math.random()*(25))+(97)); , I try to put (int) ((Math.random()*(25)+(97)) && ((Math.random()*(25)+(65))) but I that just my guess but when I put it into the code, it just say ")" missing even though I put one. In all, I have no idea how to generate random ASCII of uppercase and lowercase letter.

 
Kathleen Angeles
Ranch Hand
Posts: 123
Firefox Browser Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can post your complete code, and below that, the exact complete compiler error message (cut it and paste here)? This way, we will know what broke your code.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh sorry for not being clear. I'm just asking how can I generate random uppercase and lowercase letter without using array. I just wrote this part of the code so I can explain easier. Thanks
 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The && is used only in boolean statements, what you have is an int.

Try:

 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Won't that create an array? Is there anyway to make Java generate a number between two set of range? Like generating a mumber from 65 to 90 or 97 to 122.
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Mize wrote:Won't that create an array? Is there anyway to make Java generate a number between two set of range? Like generating a mumber from 65 to 90 or 97 to 122.


Why not generate a number between 65 and 122 and just reject it and run it again if it is between 90 and 97

Hint:


It's kind of inelegant and using an array of legitimate char values might be more simple in the end.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could also generate a single random number between 65 and 90. Generate a second random number that is either 0 or 1. If it is 1, add 32 to the first number, otherwise don't.
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would just have a big ass string (yes big ass is the technical term here) of allowable characters, then randomly pick an index. Something like this



This way, you can change the allowable characters easily.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Phil, won't I have to force the while loop (randomLowUp > 90 && randomLowUp < 97) to start by initializing randomLowUp to a value like 92? Thanks
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:I would just have a big ass string (yes big ass is the technical term here) of allowable characters, then randomly pick an index. Something like this



This way, you can change the allowable characters easily.


I was thinking of such method but won't this be a hassle method once you're require to have more ASCII included? Like numbers and punctuations. Thanks
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just change it to



It takes like 2 secs
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:You just change it to



It takes like 2 secs


True, thanks for the suggestion, it just I haven't learn.charAt and trying to understand how to do with loop and Math.random().
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Mize wrote:Hey Phil, won't I have to force the while loop (randomLowUp > 90 && randomLowUp < 97) to start by initializing randomLowUp to a value like 92? Thanks


No, just initialize your random number by calling the random generator once prior to the loop. If the number meets your criteria then it you will never enter the loop.

 
Winston Gutkowski
Bartender
Pie
Posts: 10253
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Mize wrote:True, thanks for the suggestion, it just I haven't learn.charAt and trying to understand how to do with loop and Math.random().

Well, Jayesh's suggestion looks like it uses java.util.Random (←click) instead, which is what I would also advise.

Math.random() is old hat, and actually uses Random under the hood now anyway.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic