File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes storing a set of integers in an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "storing a set of integers in an array" Watch "storing a set of integers in an array" New topic
Author

storing a set of integers in an array

Khair Bahir
Greenhorn

Joined: Aug 01, 2011
Posts: 14
Hello,

I'm trying to generate 50 random integers between 0 and 10 using a 'for' construct and the 'Math.random()' method. I know how to do this, but I don't know how to store all these integers in an array (one dimension). I really only know how to store integers by manually inputting them into the array. I haven't made much progress and could use some help.

This was my attempt (it doesn't work):



Thanks.
Abhinav Kumar Mishra
Greenhorn

Joined: Jul 25, 2011
Posts: 6
kae cee wrote:Hello,

I'm trying to generate 50 random integers between 0 and 10 using a 'for' construct and the 'Math.random()' method. I know how to do this, but I don't know how to store all these integers in an array (one dimension). I really only know how to store integers by manually inputting them into the array. I haven't made much progress and could use some help.

This was my attempt (it doesn't work):



Thanks.


hi

In the above code double value returned by random() function is converted into int before multiplication with 11.So we have to put the statement as
array[j]=(int)(Math.random()*11);
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

When you say "it doesn't work"...what does happen?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

do you know your operator precedence? Specifically, which is higher - a cast, or multiplication?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Khair Bahir
Greenhorn

Joined: Aug 01, 2011
Posts: 14
Yeah, I know now that casts take higher precedence than multiplication operators, which is why the program just displayed a bunch of zeroes when I omitted a set of parentheses. Thanks for your guys' help.
Pradeep allada
Greenhorn

Joined: Jul 28, 2011
Posts: 17
have you tried instructions given by Mr. Abhinav Kumar Mishra,
check it once
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: storing a set of integers in an array
 
Similar Threads
storing random no in Array...wiDout repetition
Arranging members of array in a ascending order?
sort even/odd integers of an array into their own arrays
How do the values change even if I am not passing the arrays with reference?
2-Dimensional Arrays?