File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes setting a variable with a random number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "setting a variable with a random number" Watch "setting a variable with a random number" New topic

setting a variable with a random number

Jeff Ciaccio

Joined: Nov 26, 2008
Posts: 25
I am trying to set variable "addr" to some random int, but I cannot seem to pass the random int in. Can anybody help me?

Jeff Ciaccio, Java novice <br />Physics and AP Physics Teacher<br />Sprayberry High School <br />Marietta, GA
Henry Wong

Joined: Sep 28, 2004
Posts: 20535

This isn't valid Java. In a method declaration, the parameters are type and name of params -- you can't put method calls there.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Well first off, you are trying to execute a statement in a parameter:

This ising really going to work, firstly i could be wrong but its an illegal statement and secondly, even if it was legal randNum hasnt been initiallised:

So given that what do you think would help you to solve your problem?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 684
It's not clear to me how your setAddr and getAddr methods are being called.

However, if setAddr method just sets the value of the instance variable addr, then presumably getAddr is going to return the value of your instance variable to some other method or class. So shouldn't your getAddr() method just return addr, and not actually set its value a second time?

p.s. ok two people got in ahead of me as I was typing my note.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15094

Your main() method contains some strange things. For example, the following lines of code:

What do you think this code does, why have you included it? It does not "create variable names cpu0, cpu1, etc." as the comment says.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: setting a variable with a random number
It's not a secret anymore!