Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: need help

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

    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);
    }
     
     
     
     
       }
     }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need help

    idk how to sort them
    Look at the Collections class. It has sort methods.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: need help

    if i use collection.sort(numbers) it just prints the same 5 numbers every time.
    51 52 53 54 55

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: need help

    What's in the collection before you sort it?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    naz585 (December 16th, 2012)

  10. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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