aspose file tools*
The moose likes Beginning Java and the fly likes operator cannot be applied to ints. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "operator cannot be applied to ints." Watch "operator cannot be applied to ints." New topic
Author

operator cannot be applied to ints.

nathan gibson
Ranch Hand

Joined: Sep 16, 2009
Posts: 120

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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18826
    
  40

The || operator, or the logical OR operator, only works with booleans -- meaning the two operands must be boolean types.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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: 38787
    
  23
David Newton wrote: . . . Java tutorials available on Sun's Java website (free) . . .
Try here.
David Newton wrote: . . . an intro Java book . . .
Try here.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

You might look up the switch/case syntax while you're at it. I think it would work in this application.

-JD


When you do things right, people won't be sure you've done anything at all.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Janice: Not really; you can only switch on constants--not ranges.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

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
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11309
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

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

Joined: Mar 09, 2009
Posts: 600
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Janeice: Oh, I thought you were talking about the part that's messed up.
Gary Bake
Greenhorn

Joined: Sep 15, 2009
Posts: 1
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: 38787
    
  23
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
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
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
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)



nick kaushik
Ranch Hand

Joined: Sep 25, 2009
Posts: 48

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
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
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
its not working. if you guys have some advice, algorithmic or syntax, it is appreciated.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18826
    
  40

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

Joined: Sep 16, 2009
Posts: 120
i have been working on it for the last few hours, does this look anymore reasonable?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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
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
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
Sheriff

Joined: Sep 28, 2004
Posts: 18826
    
  40

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
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
Sheriff

Joined: Sep 28, 2004
Posts: 18826
    
  40

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: 1296
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
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
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
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: 1296
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
Sheriff

Joined: Sep 28, 2004
Posts: 18826
    
  40

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
Dan Walin
Ranch Hand

Joined: Nov 11, 2003
Posts: 109
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.

 
 
subject: operator cannot be applied to ints.