Forums Register Login

Nedd Help On Random Nubber Generating

+Pie Number of slices to send: Send
I am trying to write a program thats a number generator between 1 to 1000. The user is aked to input an integer.Whatever number the the user
enters return that many generated numbers between 1 to 1000.
Sorta Like Enter A Integer 8
Returns 200 310 19 72 758 663 12 22
Then it puts the integers in order..
12 19 22 72 200 310 663 758
I am having trouble using the Arrays sort..

java.io.*;
import java.util.Random;
import java.util.Arrays;
class assmt2
{

public static void main(String[] args)throws IOException{
Random generator = new Random();
int temp;

System.out.println(" Enter Integers :\n"); //prompt user

BufferedReader keyRead=new BufferedReader(new InputStreamReader(System.in));
int next=Integer.parseInt(keyRead.readLine());
temp = generator.nextInt(1000);

final int [] vals = new int[next];
for(int i =0;i<next;i++){
// System.out.println(generator.nextInt(1000));
vals[i] = generator.nextInt(1000);
System.out.println(generator.nextInt(1000));
}
Arrays.sort(vals);

// }

}
System.out.print(vals);
}
+Pie Number of slices to send: Send
First of all, your code does not compile because you have your
System.out.print(vals) outside of the main() method.
I do not see any problem with the Arrays.sort() at all.
I assume you are having problem printing out the array, if you want
to print out the sorted result you can't use System.out.print(vals)
since it will print out the Array object reference.
Instead, you have to get the content of the array. Using for..loop is one way:

Hope that helps
You get good luck from rubbing the belly of a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 903 times.
Similar Threads
Need help with my Lottery Program
Variables assigned to objects
Dice probability program, using nested loops.
Random Number Generator
need help with my assessment
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:39:41.