Max size of array know but not necessarily needed...Arrays, 2D arrays, relating int arrays and string arrays
Page 1 of 1
12 Replies - 3020 Views - Last Post: 18 December 2008 - 07:29 PM
#1
Max size of array know but not necessarily needed...
Posted 15 December 2008 - 05:03 AM
I am having a horrible time as I imagine some of you did when you first started java.
Basically I need to create a lottery program that allows a maximum of 30 tickets to be brought (each has 6 numbers).
So user1 can have 10 tickets, user2 could have 4 tickets, user3 could have 5 tickets. Not all the tickets need to be brought but each ticket needs to be associated with the userName of the person that bought the ticket.
So I assume I need a 2D array to store the tickets in? But if I declare its size does that mean it will need to be filled up with 30 tickets?
Also how would I access a ticket in the array after it was entered.
I am worried that if a second user puts their name in, it will wipe the previous name.
ALL HELP WELCOME
thank you so much
Replies To: Max size of array know but not necessarily needed...
#2
Re: Max size of array know but not necessarily needed...
Posted 15 December 2008 - 07:48 AM
//Welcome to the lottery
//Here the user can choose to draw the lottery OR pick numbers
//Pick Numbers
userName = JOptionPane.showInoutDialog("Enter your Name");
//Store this in an array as user1
//Number of tickets for current user
userTickets=JOptionPane.showInputDialog("How many tickets");
//Depending on the number of tickets they want (e.g. 5 tickets)
//Choose numbers
for (int loop = 0; loop< tickSize; loop++){
userTickets = JOptionPane.showInputDialog("Please enter a number");
//Show the ticket to the user
output = "Your Ticket ";
for int loop = 0; loop<6; loop++){
output = output+userNumbers[loop]+" ";
//print message
JOptionPane.showMessageDialog(null, output, "Lottery" , JOptionPane.INFORMATION_MESSAGE);
}
//THIS USER WANTED 5 TICKETS SO ONCE THE 5X LOOP IS UP - show message to say draw lotto now or new user
//if new user, need to store this new user name in an array
//ask how many tickets they want
//the max number of tickets is 20 regardless of num of users
//
//once 20 tickets bough, lotto is drawn using random number generator, these 6 numbers are compared to all the rows in the 2D array
//winning lines are printed along with the user name who chose the line
#3
Re: Max size of array know but not necessarily needed...
Posted 15 December 2008 - 08:36 AM
You could then create a separate Ticket class with fields to hold the tickets number and the person who gets the ticket. The array would then contain each of these tickets, and when a new ticket is needed add it into the array using the previously mentioned variable, then increment this variable.
Quote
Using a 2d array is another way to do it with the column one being the ticket, and column 2 the person with the ticket. But it will probably be easier to have the Ticket objects contaiing the person who owns the ticket.
If you declare the size of the array to 30 this just means that space is allocated for an array of 30 elements, initially being null. There is no need to fill the array with 30 tickets, just make sure to ignore the null items in the array, and note that array.length will return 30 even if the spots are null.
#4
Re: Max size of array know but not necessarily needed...
Posted 15 December 2008 - 08:53 AM
BigAnt, on 15 Dec, 2008 - 07:36 AM, said:
You could then create a separate Ticket class with fields to hold the tickets number and the person who gets the ticket. The array would then contain each of these tickets, and when a new ticket is needed add it into the array using the previously mentioned variable, then increment this variable.
Quote
Using a 2d array is another way to do it with the column one being the ticket, and column 2 the person with the ticket. But it will probably be easier to have the Ticket objects contaiing the person who owns the ticket.
If you declare the size of the array to 30 this just means that space is allocated for an array of 30 elements, initially being null. There is no need to fill the array with 30 tickets, just make sure to ignore the null items in the array, and note that array.length will return 30 even if the spots are null.
import javax.swing.JOptionPane;
public class lottery {
public static void main(String[] args) {
String userNumbersNum, output = "", userNames1;
int userInteger;
final int ROWS = 30;
final int COLUMNS = 6;
final int ARRAY_SIZE = 30;
int [][] userNumbers = new int[ROWS][COLUMNS];
int []userNumbers1 = new int[COLUMNS];
String [] userNames = new String [ARRAY_SIZE];
for (int loopN = 0; loopN < 2; loopN++ ){
userNames[loopN] = JOptionPane.showInputDialog("Enter your name");
}
//userTickets = JOptionPane.showInputDialog("How many tickets?");
//userInteger = Integer.parseInt(userTickets);
//enter 6 lottery numbers
for (int loop = 0; loop < COLUMNS; loop++) {
userNumbersNum = JOptionPane.showInputDialog("Please enter a number");
userNumbers1[loop] = Integer.parseInt(userNumbersNum);
}
output = "Your numbers are are: ";
for (int loop1 = 0; loop1 < COLUMNS; loop1++){
output = output +userNumbers1[loop1]+" ";
}
//print message
JOptionPane.showMessageDialog(null, output, "Lottery" , JOptionPane.INFORMATION_MESSAGE);
}
}
This is where I am. A lot of what you are saying does not mean anything to me as I am very very new to java and only capable of simple coding at the moment. After the user has entered the numbers I want to store them in a 2D array and then relate the ticket to the user name entered. Maybe this helps...? Thank you for your time
#5
Re: Max size of array know but not necessarily needed...
Posted 15 December 2008 - 10:21 AM
Also, For the array do you want the first row to be the numbers and the second row the user name associated with the numbers?
If so the array would have to be of type string like:
String myArray[][] = new Array[30][2];
This would create a 2d "table" of values.
Then you could get the user input something like:
do
-->Ask for users name
-->Ask for ticket number(or generate random number)
-->Store in the array
-->Ask if you would like to continue
while more tickets remaining && notDone(A boolean marking whether or not you want to specify more tickets)
#6
Re: Max size of array know but not necessarily needed...
Posted 15 December 2008 - 10:44 AM
BigAnt, on 15 Dec, 2008 - 09:21 AM, said:
Also, For the array do you want the first row to be the numbers and the second row the user name associated with the numbers?
If so the array would have to be of type string like:
String myArray[][] = new Array[30][2];
This would create a 2d "table" of values.
Then you could get the user input something like:
do
-->Ask for users name
-->Ask for ticket number(or generate random number)
-->Store in the array
-->Ask if you would like to continue
while more tickets remaining && notDone(A boolean marking whether or not you want to specify more tickets)
I want to put the users numbers e.g. 1,2,3,4,5,6
into a 2D array
so number 1 would go into 0,0
number 2 would go into 0,1
I also want to to ask for the users name and then put this in an array...when they have entered their name i then want to ask them for how many tickets and then ask for their numbers at the moment when i ask for users name it keeps looping to populate.
import javax.swing.JOptionPane;
public class returnArray {
public static void main(String[] args) {
String output;
String [] usersData;
//get usersData
usersData = readUsersData();
//print array
output = "UsersNames: ";
for(int loop = 0; loop <usersData.length; loop++){
output = output +usersData[loop]+"";
}
JOptionPane.showMessageDialog(null, output, "Arrays", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}//end main
//user defined method
public static String [] readUsersData(){
String nameString;
final int ARRAY_SIZE = 5;
String [] tempArray = new String[ARRAY_SIZE];
for (int loop = 0; loop<tempArray.length; loop++){
//read in num1 and convert to integer
nameString = JOptionPane.showInputDialog("Enter your name");
tempArray[loop] = nameString;
}
return tempArray;
}//end this method
}//end class
This post has been edited by gdanelian: 15 December 2008 - 10:52 AM
#7
Re: Max size of array know but not necessarily needed...
Posted 15 December 2008 - 10:57 AM
Quote
into a 2D array
so number 1 would go into 0,0
number 1 would go into 0,1
So each column in the array would represent a list of numbers corresponding to one person from the array of Names?
This would not be a good approach, because it will complicate things since each person can have a different ammount of tickets, which is not known at the beggining and there can be up to 30 different people, which requires an array of 30 * 30 = 900 elements of which only 30 will be actually used.
#8
Re: Max size of array know but not necessarily needed...
Posted 15 December 2008 - 11:09 AM
BigAnt, on 15 Dec, 2008 - 09:57 AM, said:
Quote
into a 2D array
so number 1 would go into 0,0
number 1 would go into 0,1
So each column in the array would represent a list of numbers corresponding to one person from the array of Names?
This would not be a good approach, because it will complicate things since each person can have a different ammount of tickets, which is not known at the beggining and there can be up to 30 different people, which requires an array of 30 * 30 = 900 elements of which only 30 will be actually used.
Ok I really do not understand your advice.
If a user enters 6 numbers e.g. 1,2,3,4,5,6
these would need to be stored in an array
Their username would need to be stored in an array
1,2,3,4,5,6 is an array[6]
if the user entered another line
3,4,5,6,7,8
then i wanto add this to the array where the first line is stored
array [6][2]
[6]=the number of columns
[2]=the number of current rows max is [20]
you cannot have an array containing both string and integers anyway so i would need another string array to be populated by users entering their names and then somehow link the rows with the usernames.
#9
Re: Max size of array know but not necessarily needed...
Posted 15 December 2008 - 11:23 AM
The goal is to label and organize everything in a way that makes sense when you just look at it.
At the risk of adding confusion, does this make any sense at all?
// a class to hold ticket values
class Ticket {
public static final int SIZE = 6;
public int [] values = new int[SIZE];
public Ticket() {}
}
// a class to hold user values
class User {
public String name;
// note the user has tickets
public Ticket [] tickets;
// create the user with a name
public User(String name) { this.name = name; }
}
User [] users = = new User[ARRAY_SIZE];
for (int loopN = 0; loopN < 2; loopN++ ){
users[loopN] = new User(JOptionPane.showInputDialog("Enter your name"));
int userTicketCount = Integer.parseInt(JOptionPane.showInputDialog("How many tickets?"));
users[loopN].tickets = new Ticket[userTicketCount];
}
Now, given a User object, I have their name and all the "tickets" that related to them. It's self contained in the object. There a lot of other logic that the object can hold, like how it should print out by default ( using the toString method ) or, and this may be important, if one ticket is equal to another.
Hope this helps.
#10
Re: Max size of array know but not necessarily needed...
Posted 15 December 2008 - 11:47 AM
baavgai, on 15 Dec, 2008 - 10:23 AM, said:
The goal is to label and organize everything in a way that makes sense when you just look at it.
At the risk of adding confusion, does this make any sense at all?
// a class to hold ticket values
class Ticket {
public static final int SIZE = 6;
public int [] values = new int[SIZE];
public Ticket() {}
}
// a class to hold user values
class User {
public String name;
// note the user has tickets
public Ticket [] tickets;
// create the user with a name
public User(String name) { this.name = name; }
}
User [] users = = new User[ARRAY_SIZE];
for (int loopN = 0; loopN < 2; loopN++ ){
users[loopN] = new User(JOptionPane.showInputDialog("Enter your name"));
int userTicketCount = Integer.parseInt(JOptionPane.showInputDialog("How many tickets?"));
users[loopN].tickets = new Ticket[userTicketCount];
}
Now, given a User object, I have their name and all the "tickets" that related to them. It's self contained in the object. There a lot of other logic that the object can hold, like how it should print out by default ( using the toString method ) or, and this may be important, if one ticket is equal to another.
Hope this helps.
At this stage in my course I am only supposed to be using one class, I am using Eclipse.
#11
Re: Max size of array know but not necessarily needed...
Posted 15 December 2008 - 12:20 PM
gdanelian, on 15 Dec, 2008 - 12:47 PM, said:
???
Java programmers make internal, one off classes all the time. It doesn't mean you need another file, you can make them internally to the main class. Java without classes is like C without pointers; you've pretty much missed the idea. A String is a class, an array is a class, a... nm, I'm genuinely sad for you.
Without the ability to use the language as intended, your problem becomes far more difficult. It's basically a series of work arounds.
You could use a Hashtable for user - ticket paring, but you're still stuck with 2d arrays, which are bad form.
Maybe something like:
Hashtable users = new Hashtable();
for (int loopN = 0; loopN < 2; loopN++ ){
String userName = JOptionPane.showInputDialog("Enter your name");
users.put( userName, new Hashtable() );
No, still kludgy. You'd be better off using an associative array in PHP or Python...
Ask the instructor again, because the problem clearly lends itself to it. Good luck.
#12
Re: Max size of array know but not necessarily needed...
Posted 18 December 2008 - 06:40 PM
#13
Re: Max size of array know but not necessarily needed...
Posted 18 December 2008 - 07:29 PM
|
|

New Topic/Question
Reply




MultiQuote





|