This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
i know what my problem is, but i dont know how to fix it. i am doing an assignment that requires me to make a password generator with these instructions:
Create a randomly generated password from the selected character sets.
so i thought the best way to do it would be to calculate a random number between the two types of characters that i would need in ascii, but when i need more that one type ex: lowercase and uppercase the statement: int randomNumber = ((int)(0+ Math.random()* 65 - 90 || 97 - 122)); wont work. i dont know if i make any sense, but if more elaboration is needed please ask. thank you in advance for any help.
Be careful where you put your curly braces and beware of off-by-one errors.
I'm curious what Java book is being used or where you're getting some of your syntax ideas from--it might be worth reviewing the basics of Java expressions.
nathan gibson
Ranch Hand
Joined: Sep 16, 2009
Posts: 120
posted
0
David Newton wrote:Be careful where you put your curly braces and beware of off-by-one errors.
I'm curious what Java book is being used or where you're getting some of your syntax ideas from--it might be worth reviewing the basics of Java expressions.
all of my syntax are a direct result of taking a class with no teacher a.k.a. distance learning in highschool and no one in the school knows how to program.
I'd consider spinning through some of the Java tutorials available on Sun's Java website (free) or grabbing an intro Java book--it's worth the time to get some of the underlying concepts dialed in.
And kudos on undertaking this on your own.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
4
posted
0
David Newton wrote: . . . Java tutorials available on Sun's Java website (free) . . .
the "||" means a boolean OR. the only allowed values on either side are TRUE or FALSE. You have integers.
I understand what you are trying to do, but Java does not support it. when Java sees something like "97 - 122", it sees that as a mathematical expression that must be evaluated - and does so to make this "negative twenty five".
the "||" means a boolean OR. the only allowed values on either side are TRUE or FALSE. You have integers.
I understand what you are trying to do, but Java does not support it. when Java sees something like "97 - 122", it sees that as a mathematical expression that must be evaluated - and does so to make this "negative twenty five".
where <something> is whatever "(int)(0+ Math.random()[b]* 65" evaluates to - and that will be different each time.
I don't see how if statements are better or would work differently in this case. I understand that the random number code won't work. But applying your logic it wouldn't work with the "if" statements either....
My suggestion was to eliminate some of the confusion with the curly braces (and shed some light on a better way to deal with "if choice == 1", "if choice == 2" etc.). I had assumed with the prior threads the random number generator was already off the table.
Sorry.
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
posted
0
Janeice:
They weren't saying that the switch wouldn't work. They were saying that just changing to a switch doesn't fix the problem.
@Janeice: Oh, I thought you were talking about the part that's messed up.
Gary Bake
Greenhorn
Joined: Sep 15, 2009
Posts: 1
posted
0
Couple of points that may help
You need the opening bracket after the while statement.
The main discussion is about the line working out the number
The following will generate a number between 97 and 122 (25+97).
(int)(Math.floor(Math.random() * 25 + 97)
This is ok for the first option.
for the others you need to generate a random check so it picks numbers in the first, second, third set of numbers.
put the following at the start of your code
Random random = new Random();
For option 2 for example you can put
Hopefully this should work.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
4
posted
0
Welcome to JavaRanch
There is more discussion about "random" numbers and a more elegant way to generate numbers in a range on this recent thread.
Greg Stevens
Ranch Hand
Joined: Jul 23, 2009
Posts: 41
posted
0
nathan gibson wrote:
but when i need more that one type ex: lowercase and uppercase the statement:
int randomNumber = ((int)(0+ Math.random()* 65 - 90 || 97 - 122)); wont work.
These different types of ASCII characters are not consecutive. Do you know how to use arrays, and are they allowed
in the assignment? If so, you could create four seperate arrays (one for each type of password) and fill each array
with all of the possible values for that set. Then you could use a nested switch statement (based on the menu selection)
to determine from which array to select a random value each time through the loop.
nathan gibson
Ranch Hand
Joined: Sep 16, 2009
Posts: 120
posted
0
im not familiar with arrays, and he hasnt went over them, so i dont think it would be a good idea to use them.
Greg Stevens
Ranch Hand
Joined: Jul 23, 2009
Posts: 41
posted
0
Identifying the different ranges I get:
lowersStart = 97
lowersEnd = 122
uppersStart = 65
uppersEnd = 90
numbersStart = 48
numbersEnd = 57
mixedStart = 33
mixedEnd = 126
I notice in your code above you have 58-63 for "punctuation". That doesn't include all punctuation
characters; for example, '!' is 33. It seems more likely that "punctuation" would be all non-alphanumeric
characters. Since the menu choice with "punctuation" also includes, lowercase, uppercase, and numbers, the range
would be 33-126. These end/start identifiers make the code easier to work with, especially when it comes to identifying
ranges for random number generation.
This seems clumsy to me, but one way you could deal with the "non-consecutive" sets would be with a loop that
generates a number from within the entire range beginning with the start of the first set and ending with the end of the last
set. For example, menu choice 2 calls for uppercase and lowercase letters (65-90 and 97-122). Within your
loop that is counting the passwordLength, for menu choice 2, you could set up another loop that generates
random numbers in the range 65-122 until number is within 65-90 or number is within 97-122. That is where the
|| operator would fit in:
So the the loop says "while number is neither in uppercase range nor in lowercase range, give me
another number within the entire range." I don't know how this would affect the randomness of the password, but eventually
you will get a number within one of the two ranges.
You could do something similar with menu choice 3. And of course menu choice for calls for an entirely consecutive
set of numbers (mixedStart to mixedEnd)
Guys its ok with your loop & syntax problems but i think their is something else which is greatly wrong with this program,if i am not greatly mistaken.their is no way in your program to append the no. generated in each iteration with the original.i mean if password length is 5,then how do your program appends it to the original variable randomno. ?
or i am not understanding it well?
"ye shall know the truth & the truth shall set you free..."
Greg Stevens
Ranch Hand
Joined: Jul 23, 2009
Posts: 41
posted
0
You're right. I don't think he's gotten that far. There is no variable to store the characters that
correspond to the ASCII numbers generated in the loop.
nathan gibson
Ranch Hand
Joined: Sep 16, 2009
Posts: 120
posted
0
alright. i have been working on this assignment on and off. i have a skeleton of this going on. this is the update.
nathan gibson
Ranch Hand
Joined: Sep 16, 2009
Posts: 120
posted
0
its not working. if you guys have some advice, algorithmic or syntax, it is appreciated.
Not really. Don't you want to determine the nature of the character inside the loop instead of just once?
nathan gibson
Ranch Hand
Joined: Sep 16, 2009
Posts: 120
posted
0
okay okay, i almost have this. i have been working for hours. i got it back down to its original problem, i have everything else 100% right. alright my problem is that i cant find a way to pick a random number from two different ranges ex: 34 - 50 && 20- 30.
nathan gibson
Ranch Hand
Joined: Sep 16, 2009
Posts: 120
posted
0
the reason i am asking is because i am trying to use both capital and lowercase letters from the ascii chart and i dont know how to make a random number that will cycle through both of these but skip over the characters that are in the middle.
You still have the same problem -- with all of your loops.
If lowercase is true, you code will enter the loop. In this loop, you change the password and counter variable, but lowercase is not changed -- hence, this will be an endless loop.
In other words, if any of the loops in your program runs, it will just keep running those loops forever.
Henry
nathan gibson
Ranch Hand
Joined: Sep 16, 2009
Posts: 120
posted
0
Henry Wong wrote:You still have the same problem -- with all of your loops.
If lowercase is true, you code will enter the loop. In this loop, you change the password and counter variable, but lowercase is not changed -- hence, this will be an endless loop.
In other words, if any of the loops in your program runs, it will just keep running those loops forever.
Henry
your code sample is dated. i wasnt asking about that anyways. here is my current code:
the reason i am asking is because i am trying to use both capital and lowercase letters from the ascii chart and i dont know how to make a random number that will cycle through both of these but skip over the characters that are in the middle.
I don't think that it is possible to do it with one formula expressions. Two options...
1. Generate a random number that is the size of the two ranges (lowercase and capital) combined. Using the value of the random (greater or less than a certain value), either process it as lower or upper (ie. two different formulas).
-- or --
2. Use the while loop, as you had before -- but get it working. Generate the number, if it is valid, exit the loop. Otherwise, loop and try again.
Henry
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
posted
0
Or you can dispense with the 'magic' ASCII numbers altogether and do something like this:
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
nathan gibson
Ranch Hand
Joined: Sep 16, 2009
Posts: 120
posted
0
Garrett Rowe wrote:Or you can dispense with the 'magic' ASCII numbers altogether and do something like this:
i like your thinking, but i think that would be frowned apon.
nathan gibson
Ranch Hand
Joined: Sep 16, 2009
Posts: 120
posted
0
Henry Wong wrote:
the reason i am asking is because i am trying to use both capital and lowercase letters from the ascii chart and i dont know how to make a random number that will cycle through both of these but skip over the characters that are in the middle.
I don't think that it is possible to do it with one formula expressions. Two options...
1. Generate a random number that is the size of the two ranges (lowercase and capital) combined. Using the value of the random (greater or less than a certain value), either process it as lower or upper (ie. two different formulas).
-- or --
2. Use the while loop, as you had before -- but get it working. Generate the number, if it is valid, exit the loop. Otherwise, loop and try again.
Henry
alright, option 1 seems simpler. its just i am having a hard time imagining how to do that, is there anyway i can get an example or a step by step? also i dont know where i would put it.
nathan gibson
Ranch Hand
Joined: Sep 16, 2009
Posts: 120
posted
0
im going to try to go this route.
im not quite sure how this is supposed to go.
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
posted
0
nathan gibson wrote:
Garrett Rowe wrote:Or you can dispense with the 'magic' ASCII numbers altogether and do something like this:
i like your thinking, but i think that would be frowned apon.
I don't understand, why would that be frowned upon. I frown upon the version with the 'magic numbers'. It's not immediately clear without looking at the comments (if there are comments) what is going on in the code. Did your instructor tell you specifically that you were to use ASCII values?
Well, you need to get the overall logic correct. You need to generate a random number that is in range first, then deal with adding it to the password. If you have the add to password in the loop, doesn't that add the invalid stuff too?
Anyway, something like this... in pseudo code ...
Henry
Dan Walin
Ranch Hand
Joined: Nov 11, 2003
Posts: 109
posted
0
Just an idea to think about: I have a method that takes a string of letters: "aaz", for example and increments it by one to get "aba", run it again and you get "abb", again and you get "abc", etc. You could take this code and modify it so that you pass one letter at a time (a starting point such as "a") and increment it based on a randomly generated number. You can do this for every position in your password. The result of each call should still be in the valid range it it's position in that range, set by a random number.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.