Results 1 to 11 of 11
  1. #1
    ErnieG014 is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Unhappy Methods, Arrays, and Loops - Oh my!

    Hello Java Forums,

    I'm a new Java developer and a beginner - I have little to no experience with Java and could really use some help while I continue to search an answer for this question on this site. Basically me and a group of people want to hold a lottery - and we don't want anyone cheating so we've decided to have a code to act as our lottery. Basically there are 26 of us, so we've decided to assign each of us a letter. We want the Java program to randomly choose 10 letters from the 26 letter filled English alphabet and to place them into a string called "Players." After the 10 letters have been chosen and placed into this "Players" string, we will then choose 5 letters out of those 10 and place those in a string called "Losers." The remaining 5 letters will be the winners out of the 26 grouped lottery. So basically we want the program to choose 10 letters randomly from 26 - place it into a string called "Players" - randomly choose 5 letters from the "Players" string and print out the winners set which are the last remaining 5 letters that haven't been chosen for the losers string. We know what to do, but we don't know how to do it and this site seemed like the best place to ask for advice. We know we have to use arrays and more than one method, but we don't know how to do this. Any help is appreciated!

    Just a quick rundown of how we want it to be printed out:

    Players set: X A C F I K E L D P

    Losers set: C I K E D

    Winners set: X A F L P

    Thank you in advanced!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Methods, Arrays, and Loops - Oh my!

    Break it down.
    You must be able to do the little bits of this.
    Start with getting the 10 letters.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Methods, Arrays, and Loops - Oh my!

    Hi Ernie, welcome to the forums.

    The guides generally refered to are Trail: Learning the Java Language.
    If you have a rough idea of where to start then a read through these would be of interest.

    Let us know if you still require additonal assistance or pointers.

    Regards.
    Last edited by Ronin; 04-03-2013 at 10:45 AM. Reason: Typo

  4. #4
    ErnieG014 is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Re: Methods, Arrays, and Loops - Oh my!

    Thanks for the warm introduction!

    This is my code so far:

    import java.util.Random;

    public class Lottery {

    public static void main(String[] args) {
    Random r = new Random();
    char A = (char) (r.nextInt(26) + 'A');
    char B = (char) (r.nextInt(26) + 'A');
    char C = (char) (r.nextInt(26) + 'A');
    char D = (char) (r.nextInt(26) + 'A');
    char E = (char) (r.nextInt(26) + 'A');
    char F = (char) (r.nextInt(26) + 'A');
    char G = (char) (r.nextInt(26) + 'A');
    char H = (char) (r.nextInt(26) + 'A');
    char I = (char) (r.nextInt(26) + 'A');
    char J = (char) (r.nextInt(26) + 'A');
    System.out.print("Players Set: " +A +B +C +D +E +F +G +H +I +J);
    Losers();
    }

    public static void Losers() {
    Random r = new Random();
    char K = (char) (r.nextInt(+A +B +C +D +E +F +G +H +I +J) + 'A');
    System.out.print("Losers Set: " +K);
    }
    }

    I know I should be using arrays, but I don't really know how. Any tips?

    Thanks!

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Methods, Arrays, and Loops - Oh my!

    Since you asked so nicely.
    Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

    And as you will probably want to loop through the arrays at some point.
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Regards.

  6. #6
    ErnieG014 is offline Member
    Join Date
    Apr 2013
    Posts
    3
    Rep Power
    0

    Default Re: Methods, Arrays, and Loops - Oh my!

    Thank you for the tips! I've progressed a lot in this program - and I have the wonderful help of this site to thank!

    Here is my code so far:

    Java Code:
     import java.util.Random;    
        
    public class tester {    
      
    public static void main(String[] args) {    
    Random r = new Random();    
      
        System.out.print("Players Set: " );  
    char[] array = new char[9];  
    for(int t = 0; t < 9; t++){  
         array[t] = (char) (r.nextInt(26) + 'A');  
         System.out.print(array[t] + " ");  
    }    
    System.out.println("");  
    Losers(array);    
      
    }    
      
    public static void Losers(char[] array) {    
         Random r = new Random();   
    int i =0;  
    System.out.print("Losers Set: " );  
    while (i  < 5){  
    int k = r.nextInt(9);    
    System.out.print(  array[k] + " ");    
    i++;  
    }  
    }    
    }
    What I need to figure out now is how to select the winners from the Players set while not including the Losers set. What do you suggest I use for this?

    Thank you for all of your help!

  7. #7
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    317
    Rep Power
    4

    Default Re: Methods, Arrays, and Loops - Oh my!

    A quick and basic way of achieving what you want would to use an additional two arrays of five elements each. One array would hold the winners whilst the other the losers.
    Iterating through the players array you could use the Math.Random() function to randomly assign the value of an element to either the winners or loses array. Keep a note of the number of chars which have been assigned to each array as the application may try to assign too many to one array.

    Please note this is a quick and simple way of answering your question. There are more efficient ways using alternative methods but I'll leave it at that for now.

    I also noticed you initially stated you wanted ten letters when you code only creates nine. Are you confusing the number of elements with the position of the last index?

    Regards.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Methods, Arrays, and Loops - Oh my!

    Smells like homework to me!

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Methods, Arrays, and Loops - Oh my!

    Quote Originally Posted by Junky View Post
    Smells like homework to me!
    Well, yes, but the OP is doing a pretty good job of doing their own work with only a few pointers from out side of things.
    We don't frown on someone asking for help with their homework if they show willing to actually do the work themselves.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Methods, Arrays, and Loops - Oh my!

    If only that darn Collections.shuffle( ... ) method would return the shuffled list, it would've been a one-liner ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Methods, Arrays, and Loops - Oh my!

    Quote Originally Posted by Tolls View Post
    We don't frown on someone asking for help with their homework
    That was not my issue. In the OP, homework was never mentioned and it was worded as they were working on their own pet project. Some people seem to think they are more likely to get help if it is not homework and try and fool us in to helping them. But all they have to do is be honest and they will get plenty of help. Try and deceive and get nothing but scorn.

Similar Threads

  1. Loops and arrays
    By Katniss in forum New To Java
    Replies: 1
    Last Post: 10-19-2012, 05:21 PM
  2. Arrays and for-loops??
    By esined93 in forum New To Java
    Replies: 2
    Last Post: 10-17-2012, 03:24 PM
  3. Need help with methods and 'for' loops?
    By runnerandreader in forum New To Java
    Replies: 4
    Last Post: 10-05-2011, 10:25 PM
  4. Replies: 8
    Last Post: 03-28-2011, 12:06 PM
  5. How to use arrays and loops?
    By asadzarrar in forum New To Java
    Replies: 1
    Last Post: 10-31-2010, 11:04 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •