• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

operator cannot be applied to ints.

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The || operator, or the logical OR operator, only works with booleans -- meaning the two operands must be boolean types.

Henry
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 80777
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote: . . . Java tutorials available on Sun's Java website (free) . . .

Try here.

David Newton wrote: . . . an intro Java book . . .

Try here.
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might look up the switch/case syntax while you're at it. I think it would work in this application.

-JD
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Janice: Not really; you can only switch on constants--not ranges.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nathan:

You might want to check out the javadoc for the Random class. Random has methods for generating random ints, so you won't have to do casts.

John.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:@Janice: Not really; you can only switch on constants--not ranges.



Why won't this work:



It gets rid of a trillion curly braces, and makes it easier to read. I thought the switch/case was to get rid of daisy chained if statements.....

--Janeice
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because this is meaningless:

int randomNumber = ((int)(0+ Math.random()* 65 - 90 || 97 - 122 || 48- 57 || 58 - 63));

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".

so now you have

int randomNumber = ((int)(0+ Math.random()* -25 || -25 || -9 || -5 ));

Java then tries to evaluate the 'ORs'. -25 OR -25 is not valid - there's some value other than TRUE or FALSE on either side.

Note: What i wrote is not techinically correct, as I believe the * will have precedence, so you'll actually end up with

int randomNumber = (<something> - 90 || -25 || -9 || -5 ));

where <something> is whatever "(int)(0+ Math.random()[b]* 65" evaluates to - and that will be different each time.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:because this is meaningless:

int randomNumber = ((int)(0+ Math.random()* 65 - 90 || 97 - 122 || 48- 57 || 58 - 63));

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".

so now you have

int randomNumber = ((int)(0+ Math.random()* -25 || -25 || -9 || -5 ));

Java then tries to evaluate the 'ORs'. -25 OR -25 is not valid - there's some value other than TRUE or FALSE on either side.

Note: What i wrote is not techinically correct, as I believe the * will have precedence, so you'll actually end up with

int randomNumber = (<something> - 90 || -25 || -9 || -5 ));

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
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

John.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Janeice: Oh, I thought you were talking about the part that's messed up.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80777
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)



 
Ranch Hand
Posts: 48
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greg Stevens
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its not working. if you guys have some advice, algorithmic or syntax, it is appreciated.
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take this while loop...



If the lowercase boolean flag is set, it will generate a new random number -- which is then casted to a letter variable. But...

1. The letters variable is not used (while it is in scope). So, the assignment doesn't do anything.

2. The lowercase variable is *not* changed in the loop, so it is still true. In other words, this loop, if entered, will be an endless loop.

Henry
 
nathan gibson
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have been working on it for the last few hours, does this look anymore reasonable?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not really. Don't you want to determine the nature of the character inside the loop instead of just once?
 
nathan gibson
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you can dispense with the 'magic' ASCII numbers altogether and do something like this:

 
nathan gibson
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im going to try to go this route.


im not quite sure how this is supposed to go.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nathan gibson wrote:


im not quite sure how this is supposed to go.



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
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic