This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Having a little problem with Math.random() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Having a little problem with Math.random() " Watch "Having a little problem with Math.random() " New topic
Author

Having a little problem with Math.random()

sean parsons
Ranch Hand

Joined: Oct 18, 2010
Posts: 34
SimpleDotCom theDotCom = new SimpleDotCom();
int randomNum = (int) (Math.random() * 5);
int[] locations = (randomNum, randomNum+1, randomNum+2);
theDotCom.setLocationCells(locations);
boolean isAlive = true;

and theese are the CMD line errors
SimpleDotComTestDrive.java:9: ')' expected
int[] locations = (randomNum, randomNum+1, randomNum+2);
^
SimpleDotComTestDrive.java:9: ';' expected
int[] locations = (randomNum, randomNum+1, randomNum+2);
^
SimpleDotComTestDrive.java:9: not a statement
int[] locations = (randomNum, randomNum+1, randomNum+2);
^
SimpleDotComTestDrive.java:9: ';' expected
int[] locations = (randomNum, randomNum+1, randomNum+2);
^
4 errors

Wants to change the world....
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Wait a minute. You have a syntax error on this line of code:

And from that you conclude that you have a problem with Math.random()? How on earth did you do that?

No. Skip the holistic thinking and just look at what the compiler tells you. There's a syntax error on that line of code because you can't make an array that way. Not with the round brackets. You do it like this:
sean parsons
Ranch Hand

Joined: Oct 18, 2010
Posts: 34
I changed the code to what you told me to, and now i have 7 errors.

SimpleDotComTestDrive.java:9: array dimension missing
int[] locations = new int[] (randomNum, randomNum+1, randomNum+2);
^
SimpleDotComTestDrive.java:9: not a statement
int[] locations = new int[] (randomNum, randomNum+1, randomNum+2);
^
SimpleDotComTestDrive.java:9: ';' expected
int[] locations = new int[] (randomNum, randomNum+1, randomNum+2);
^
SimpleDotComTestDrive.java:9: not a statement
int[] locations = new int[] (randomNum, randomNum+1, randomNum+2);
^
SimpleDotComTestDrive.java:9: ';' expected
int[] locations = new int[] (randomNum, randomNum+1, randomNum+2);
^
SimpleDotComTestDrive.java:9: not a statement
int[] locations = new int[] (randomNum, randomNum+1, randomNum+2);
^
SimpleDotComTestDrive.java:9: ';' expected
int[] locations = new int[] (randomNum, randomNum+1, randomNum+2);
^
7 errors
sean parsons
Ranch Hand

Joined: Oct 18, 2010
Posts: 34
oh, ok i didn't see the change to the curly brackets!
sean parsons
Ranch Hand

Joined: Oct 18, 2010
Posts: 34
Thank you! it complies and runs now, thank you very much!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

When you declare and initialize at the same time you don't need the "new int[]":
That's your original code but with parentheses replaced by curly braces.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
sean parsons
Ranch Hand

Joined: Oct 18, 2010
Posts: 34
Rob Prime wrote:When you declare and initialize at the same time you don't need the "new int[]":
That's your original code but with parentheses replaced by curly braces.


Thats what i did, just replace the curve with the curly brackets, that was the only mistake, it's always something little.
 
 
subject: Having a little problem with Math.random()
 
Similar Threads
HFJ SimpleDotCom problems
Help with code
Head First Java Problem
ArrayList
getting exception in thread "main" java.lang.NoSuchMethodError: main error message