Methods, Arrays, and Loops - Oh My! Lottery question in Java.

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 654 Views - Last Post: 03 April 2013 - 06:38 PM Rate Topic: -----

#1 ErnieG014  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 13

Methods, Arrays, and Loops - Oh My! Lottery question in Java.

Posted 02 April 2013 - 06:45 PM

Hello dreamincode,

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!

Is This A Good Question/Topic? 0
  • +

Replies To: Methods, Arrays, and Loops - Oh My! Lottery question in Java.

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Methods, Arrays, and Loops - Oh My! Lottery question in Java.

Posted 02 April 2013 - 07:38 PM

So what have you tried ?

:code:
Was This Post Helpful? 0
  • +
  • -

#3 ErnieG014  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 13

Re: Methods, Arrays, and Loops - Oh My! Lottery question in Java.

Posted 02 April 2013 - 07:40 PM

To be honest, I don't even know where to begin. I'm assuming I need three methods (one named Players, another named Losers, and the main). In the players method it should randomly select 10 letters from the alphabet - but I'm not sure as how to do that. Then it's basically the same thing in the other two methods but just selecting the numbers from the previous list.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Methods, Arrays, and Loops - Oh My! Lottery question in Java.

Posted 02 April 2013 - 07:42 PM

You don't expect us to do your homework do you ?

So you'll have to post something
Was This Post Helpful? 0
  • +
  • -

#5 ErnieG014  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 13

Re: Methods, Arrays, and Loops - Oh My! Lottery question in Java.

Posted 02 April 2013 - 07:43 PM

Of course I don't expect any of you to do my homework - well this isn't even homework, haha. What do you recommend I use to find these random letters? I was thinking of using the Random class but I don't know how to declare/use it. Any pointers?
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Methods, Arrays, and Loops - Oh My! Lottery question in Java.

Posted 02 April 2013 - 07:48 PM

Use ther Random class nextInt(26) method

http://docs.oracle.c...til/Random.html

it will give you a number between 0 and 25 that you can add to 'A' to have a letter between 'A' and 'Z'
Was This Post Helpful? 0
  • +
  • -

#7 ErnieG014  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 13

Re: Methods, Arrays, and Loops - Oh My! Lottery question in Java.

Posted 02 April 2013 - 07:51 PM

Thanks for the tip - this is what I currently have so far:

 import java.util.Random;

public class Lottery {
	
	public static void main(String[] args) {
		Random r = new Random();
		char c = (char) (r.nextInt(26) + 'a');
	}
} 


How do I get the program to print out the result?

(Sorry about all of this - I'm extremely new at programming).
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Methods, Arrays, and Loops - Oh My! Lottery question in Java.

Posted 02 April 2013 - 07:56 PM

System.out.println("The char is: " + c );

This post has been edited by pbl: 02 April 2013 - 07:56 PM

Was This Post Helpful? 0
  • +
  • -

#9 ErnieG014  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 13

Re: Methods, Arrays, and Loops - Oh My! Lottery question in Java.

Posted 02 April 2013 - 08:00 PM

Here is my code so far - I think with this basic knowledge I can finish it.

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


Thank you so much!
Was This Post Helpful? 0
  • +
  • -

#10 ErnieG014  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 13

Re: Methods, Arrays, and Loops - Oh My! Lottery question in Java.

Posted 02 April 2013 - 08:23 PM

Well, it seems I got my hopes up too high - I'm currently stuck.

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


This is what I have so far for my code, I'm trying to get a randomly selected letter from the "Players set" but it won't work. Any ideas?
Was This Post Helpful? 0
  • +
  • -

#11 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Methods, Arrays, and Loops - Oh My! Lottery question in Java.

Posted 03 April 2013 - 04:36 AM

I'd suggest collecting the first 10 people in an array[10], then dividing them up into 2 arrays[5], one being winners and one being losers. Do this randomly. Do this itterativly (for loops).

You can do this with a list of names, if this is legitimately not homework, have a loop assigning 26 names to a string array, loop through that array by using a unique random number(store each random and compare them to the new random), until you have 10 candidates.

Repeat the random selection process until you have 5 winners, the rest are losers.
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7572
  • View blog
  • Posts: 12,717
  • Joined: 19-March 11

Re: Methods, Arrays, and Loops - Oh My! Lottery question in Java.

Posted 03 April 2013 - 06:51 AM

Yes, this is going to be pretty impossible without using an array. Okay, possible, but extremely unpleasant.

Let's start simple. How would you go about filling an array of capacity 10 with randomly selected integers, with no repeats? If you don't know how to declare an array, read this.
Was This Post Helpful? 0
  • +
  • -

#13 ErnieG014  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-April 13

Re: Methods, Arrays, and Loops - Oh My! Lottery question in Java.

Posted 03 April 2013 - 12:57 PM

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:

 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!
Was This Post Helpful? 0
  • +
  • -

#14 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Methods, Arrays, and Loops - Oh My! Lottery question in Java.

Posted 03 April 2013 - 02:29 PM

For a start, new char[9]; will only give you an array indexed from 0-8 (9 characters), not index 0-9 (10 characters). Don't get caught out there.

You can use a set to generate unique random numbers as sets do not allow duplicates. Here's an example:

while(set.size > 10)
  set.add((int)(Math.random()*26));


If any duplicates appear, they will be rejected and the size of the set will not increase.

Now you have 10 unique random numbers between 1 and 26. Enjoy.
Was This Post Helpful? 0
  • +
  • -

#15 FallenG  Icon User is offline

  • New D.I.C Head

Reputation: 22
  • View blog
  • Posts: 44
  • Joined: 12-January 13

Re: Methods, Arrays, and Loops - Oh My! Lottery question in Java.

Posted 03 April 2013 - 02:54 PM

If you generate the letters independently of each other, there is no guarantee they won't be repeats. What would happen if the five winners were all A, and the five losers were also all A?

Since you used the word 'set' in your description, and if it isn't too much a change of direction, I would suggest using a Java Set to hold your winners and losers.

// Create set of A-Z
// Remove 5 random elements and place into Set `winners`
// Remove 5 random elements and place into Set `losers`

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2