wood burning stoves 2.0*
The moose likes Java in General and the fly likes Nedd Help On Random Nubber Generating Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Nedd Help On Random Nubber Generating" Watch "Nedd Help On Random Nubber Generating" New topic
Author

Nedd Help On Random Nubber Generating

Alan Green
Greenhorn

Joined: Sep 07, 2003
Posts: 17
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);
}
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Nedd Help On Random Nubber Generating