Win a copy of Penetration Testing Basics this week in the Security forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nedd Help On Random Nubber Generating

Alan Green
Posts: 17
  • 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..*;
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(;
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);

// }

Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • 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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic