Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Having a little problem with Math.random()

 
sean parsons
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, ok i didn't see the change to the curly brackets!
 
sean parsons
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you! it complies and runs now, thank you very much!
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sean parsons
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic