Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nedd Help On Random Nubber Generating

 
Alan Green
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
Ruby Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic