0

I'm trying to create a lotto, where you have to read in a data file and match the numbers read in to winning numbers given through the console, my problem is that in the for loop where i read in the players numbers, it reads in fine, i even did a

 system.out.print(ticketList.get(i).getNumbers()[j]+" "); 

that checked if the numbers were correct, which they were, but when i did the same line of code outside the for loop where i add the instance of a ticket to the arraylist

 ticketList.add(new Ticket(Players[i],ticketnumbers));`

It all gets over taken by the last instance ticket that was created, but the wierd part is, its only the player numbers that get over taken, not the player names, so thats what really threw me off, I've been sitting on it for a while trying to find stuff, but I've come up empty so far, any help would be appreciated!

I wanna say that the rest of the code works, because if i put the winning numbers as the last persons numbers he wins the lotto lol. I just know that when i try to match the number arrays it only uses the last persons numbers instead of everyone's individual numbers, so thats why that piece of code does not work.

public class Lottery{

static ArrayList<Ticket> ticketList = new ArrayList(); 

public static void scanFile() throws FileNotFoundException 
{
    String fileName;
    int[] WinningNumbers = new int[6];
    Scanner scan = new Scanner(System.in);

    System.out.println("enter a string");
    fileName = scan.nextLine();
    System.out.println("Enter the winning Lottery Numbers");
    for(int i =0; i<WinningNumbers.length;i++)
    {
        WinningNumbers[i] = scan.nextInt();
    }
    scan.close();

    int NumberofTickets;
    File file = new File(fileName);
    Scanner scanInput = new Scanner(file);
    NumberofTickets = scanInput.nextInt();
    scanInput.nextLine();

    String[] PlayersName = new String[NumberofTickets];
    int[] ticketnumbers = new int[6];

    for(int i=0; i < NumberofTickets;i++)
    {
        scanInput.nextLine();
        PlayersName[i] = scanInput.nextLine();
        scanInput.nextLine();
        for(int j = 0 ; j<6;j++)
        {
            ticketnumbers[j] = scanInput.nextInt();

        }

        if(i != NumberofTickets-1)
        scanInput.nextLine();

        ticketList.add(new Ticket(PlayersName[i],ticketnumbers));
        for(int j = 0 ; j<6;j++)
        {   
            System.out.print(ticketList.get(i).getNumbers()[j]+" ");
            }
    }
    for(int i=0; i < NumberofTickets;i++)
    {
        for(int j = 0 ; j<6;j++)
        {   
            System.out.print(ticketList.get(i).getNumbers()[j]+" ");
            }
    }
    checkTickets(WinningNumbers,NumberofTickets);
    scanInput.close();
}


public static void checkTickets(int[] winningNumbers, int NumberofTickets)
{

    int[] winnersMatchedNumbers = new int[6];
    for(int i =0; i<NumberofTickets; i++)
    {
        int counter = 0;
            if(winningNumbers[j] == ticketList.get(i).getNumbers()[j])
            {
                counter = counter+1;

            }
        }
        winnersMatchedNumbers[i] = counter;

    }

    Winners(winnersMatchedNumbers,NumberofTickets);
}
public static void Winners(int[] matchedNumbers, int NumberofTickets)
{
    for(int i = 0; i<NumberofTickets;i++)
    {
        System.out.println(ticketList.get(i).getTicketName()+ " matched "+ matchedNumbers[i]+" and won "+ ticketList.get(i).getWinnings(matchedNumbers[i]));
    }
}


public static void main(String[] args) {
    // TODO Auto-generated method stub
    try {
        scanFile();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

}

the other class i am using is here

public class Ticket 
{
   private String ticketName;
   private int[] personNumber = new int[6];

public Ticket(String Name,int[] ticketNumbers)
{   
     ticketName = Name;
     personNumber = ticketNumbers;  
}
public String getTicketName()
{
    return ticketName;
}

public int[] getNumbers()
{
    return personNumber;
}
public int getWinnings(int count)
{
    int winningsAmount;
    switch(count)
    {
    case 3:  winningsAmount = 10;
                break;
    case 4:     winningsAmount = 100;
                break;
    case 5:     winningsAmount = 10000;
                break;
    case 6: winningsAmount = 1000000;
                break;
    default: winningsAmount = 0;
                break;
    }
    return winningsAmount;
}
}
Burus
  • 11
  • 5
  • 4
    Look at your code, and think about this: how many different int[] arrays (ticketNumbers) do you **create**? I.e. how many times do you call `new int[6]`? – JB Nizet Jan 19 '16 at 06:31
  • once i guess, since i used a global variable? Okay so ill switch that to make a new int[6] in the constructor and see what happens, gotta wait till i get off work, thanks for hint! – Burus Jan 19 '16 at 17:35
  • The constructor is fine. Just like you create a new Ticket at each iteration of the loop, you need to create a new int array at each iteration of the loop, because each ticket must have its own array of numbers, and not the sam array as all the other tickets. – JB Nizet Jan 19 '16 at 17:40
  • ahhh okay i see, i left int[] ticketnumbers = new int[6]; outside of my loop, and that needs to be recreated for every new ticket, but the players names dont since i put them all into one array, i think i got it now – Burus Jan 19 '16 at 17:58
  • okay, so it works for the most part, having trouble where the counter variable stops counting after the first ticket's numbers go through, so lets say i made the first persons numbers match exactly with the winning numbers, he would win according to the code, but if ticket 2 had 3 of his numbers the count would show 0 instead of 3 – Burus Jan 19 '16 at 21:31
  • i think i found the problem, it checks the position of each array and matches them together, instead of checking if the number in the ticket matches a number in the winning ticket – Burus Jan 19 '16 at 21:41
  • okay, now i definitely got it to work, thanks alot @JB Nizet !!! i do appreciate it! i did have to do 3 for loops for the matching of the numbers, not sure if there is a better way of doing it, might try to research some stuff. – Burus Jan 19 '16 at 21:51

0 Answers0