# need help

• December 16th, 2012, 06:34 PM
naz585
need help
i need a code that can print 6 random numbers. the first five numbers have to be from 51-100 and the sixth one has to be from 1-50, the numbers cannot repeat and they have to be sorted from greatest to least. i got the first two parts right but idk how to sort them after they've been shuffled. can someone please help me or give me some tips.

Code :

```import java.util.Random; import java.util.Collections; import java.util.ArrayList; public class Lottery {   public static void main(String[] args) {     ArrayList<Integer> numbers = new ArrayList<Integer>(); for(int i = 51; i < 100; i++) { numbers.add(i); }   Collections.shuffle(numbers); System.out.print("This week's lottery numbers are: "); for(int j =0; j < 5; j++) { System.out.print(numbers.get(j) + " "); } Random f = new Random(); {   int lastnum=f.nextInt(50) + 1;   System.out.print(" "+lastnum); }         } }```
• December 16th, 2012, 06:38 PM
Norm
Re: need help
Quote:

idk how to sort them
Look at the Collections class. It has sort methods.
• December 16th, 2012, 06:48 PM
naz585
Re: need help
if i use collection.sort(numbers) it just prints the same 5 numbers every time.
51 52 53 54 55
• December 16th, 2012, 06:50 PM
Norm
Re: need help
What's in the collection before you sort it?
• December 16th, 2012, 07:01 PM
naz585
Re: need help
before i used collection.sort i would get 5 random numbers from 51-100 unsorted and a 6th number from 1-50 which is always last and doesn't need to be sorted

Output:
This week's lottery numbers are: 95 55 74 65 99 43

what i want the output to be:
This week's lottery numbers are: 99 95 74 65 55 43
• December 16th, 2012, 07:23 PM
Norm
Re: need help
Does the program print out the contents of the collection before it sorts it?
What is printed?

Can you post the current version of the program?
• December 16th, 2012, 08:02 PM
naz585
Re: need help
I use JCreator LE V1.00. It does not show the contents of the collection. Below is exactly whats printed from the code above. the numbers are random everytime. I just need to find a way to put the numbers in order from greatest to least.

This week's lottery numbers are: 60 74 75 87 71 22
• December 16th, 2012, 08:05 PM
Norm
Re: need help
Print out the contents of the collection before you sort it. When you see that, you'll understand what the problem is.
• December 16th, 2012, 10:37 PM
naz585
Re: need help
I saw what was wrong in the collection, i made two arraylists shuffled the first one and then added it to the second arraylist and then sorted the second one.
ty norm