Lottery project - need help

Hello, I have a problem with my program on a lotto game, or the user has to enter as many numbers as they want into the console and thereafter there must be a random number draw (if the user has entered 6 numbers into the console then there will be 6 random numbers and the numbers must not repeat.

My code compiles without problems however it displays this to me:

Player numbers:
Edition numbers:
Number of correct numbers: 0
***** JACKPOT *****

and the code and the following:

import java.util.Arrays;

public class Loto {

    public static void main (String [] args) {

        int nbChoose [] = new int [args.length]; // array containing the numbers used by the user
        int nbLoto [] = new int [args.length]; // array containing the numbers drawn at random
        // int nbLoto [] = {1,2,3,4,5}; // Table to test the jackpot
        
        for (int a = 0; a <nbChoisis.length; a ++) {// we fill the two arrays

            try {// we check that the user has entered integers

                nbChoose [a] = Integer.parseInt (args [a]); // if all goes well we fill the nbChoose array with the data that the user has inserted

            } catch (NumberFormatException e) {

                break; // if we have an error, it means that the user has not entered only integers, so we exit the loop and display the error message located at the end of the program

            }

            nbLoto [a] = (int) (Math.random () * 49 + 1); // the random method gives a number which is between 0 and 1 so we multiply it by 49 to have a number between 0 and 49 and then we add it to 1 to have a number between 1 and 50. In addition we force it to be an integer

        }

        int nbEgal = 0; // we define a variable to check that all the numbers entered by the user are different

        for (int n = 0; n <nbChoose.length; n ++) {// we check that all the values ​​entered by the user are different

            for (int m = 0; m <nbChoose.length; m ++) {

                if (nbChoose [n] == nbChoose [m]) {

                    nbEgal = nbEgal + 1;

                }

            }

        }

        int nbCorrect = 0; // we define a variable that will allow us to see if the digits entered by the user are contained in the interval 0, 50

        for (int i = 0; i <nbChoose.length; i ++) {

            if (nbChoose [i]> 0 && nbChoose [i] <= 50) {

                nbCorrect = nbCorrect + 1; // If the numbers are contained in the interval we increment nbCorrect

            }

        }

        if (nbCorrect == nbChoisis.length && nbChoisis.length <= 5 && nbEgal == 0) {// We check that all the numbers are contained in the interval and that the user has entered a maximum of 5 numbers

            int nbWinners = 0; // We define a variable allowing us to count the number of winning numbers

            for (int x = 0; x <nbChoose.length; x ++) {

                for (int y = 0; y <nbChoose.length; y ++) {

                    if (nbChoose [x] == nbLoto [y]) {

                        nbWinners = nbWinners + 1; // we increment the nbWinners variable if the number is winners

                    }

                }

            }

            boolean jackpot = false; // We declare a boolean Jackpot to know if the user has had the jackpot or not we initialize it to no

            if (nbGagnants == nbChoisis.length) {// we check if the nbGagnants variable is equal to the total number of numbers entered by the user

                jackpot = true; // if yes we say that the jackpot is true

            }

            // we sort the arrays
            Arrays.sort (nbChoose);
            Arrays.sort (nbLoto);

            // we display the different information
            System.out.println ("Player numbers:" + Arrays.toString (nbChoisis));
            System.out.println ("Draw numbers:" + Arrays.toString (nbLoto));
            System.out.println ("Number of good numbers:" + nbWinners);

            if (jackpot == true) {// if the user has the jackpot we display the following message

                System.out.println ("***** JACKPOT *****");

            }

        } else {

            // if the user enters digits which do not correspond to the interval 0, 50 or if the user has entered more than 5 digits, this error message is displayed
            System.out.println ("You must enter whole numbers between 0 and 50 and a maximum of 5 different numbers.");

        }

    }

}

ps: i am new to java so i cant seem to solve this problem.

If anyone can help me that would be great.

thank you in advance

I formatted your code, so I could view it more clearly, and copy/paste it where I could try to compile it. For your code to display correctly in the forums, it needs to appear between otherwise blank lines that have three back ticks only on them like so:

```
code goes here
```

Anyhow, when I try to compile your code, I get the following error:

Loto.java:11: error: cannot find symbol
        for (int a = 0; a <nbChoisis.length; a ++) {// we fill the two arrays
                           ^
  symbol:   variable nbChoisis

Is this your complete code? I don’t see where you have defined an array named nbChoisis.

Thank you, but yes actually nbChoisis is defined before, but I traduced from French so it could have some issues and I will pay attention :

the actual code is (all code) :

import java.util.Arrays;

public class Loto {

    public static void main(String[] args){

        int nbChoisis[] = new int[args.length]; //tableau contenant les nombres utilisés par l'utilisateur
        int nbLoto[] = new int[args.length]; //tableau contenant les numéros tirés au sort
        //int nbLoto[] = {1,2,3,4,5}; //Tableau pour tester le jackpot
        
        for (int a = 0; a < nbChoisis.length; a++){ // on remplit les deux tableaux

            try { // on vérifie que l'utilisateur a bien rentré des entiers

                nbChoisis[a] = Integer.parseInt(args[a]); // si tout vas bien on remplit le tableau nbChoisis avec les données que l'utilisateur a inséré

            } catch (NumberFormatException e) {

                break; // si on a une erreur c'est donc que l'utilisateur n'a pas entré seulement des entiers, on sort donc de la boucle et on affiche le message d'erreur situé à la fin du programme

            }

            nbLoto[a] = (int)(Math.random()*49+1); // la méthode random donne un chiffre qui est entre 0 et 1 donc on le multiplie par 49 pour avoir un nombre entre 0 et 49 et ensuite on l'additionne à 1 pour avoir un nombre entre 1 et 50. De plus on le force à être un entier 

        }

        int nbEgal = 0; // on definie une variable pour vérifier que tout les nombres entrés par l'utilisateur sont differents

        for (int n = 0; n < nbChoisis.length; n++){ // on vérifie que toutes les valeurs entrées par l'utilisateur sont différentes

            for (int m = 0; m < nbChoisis.length; m++){

                if (nbChoisis[n] == nbChoisis[m]){

                    nbEgal = nbEgal + 1;

                }

            }

        }

        int nbCorrect = 0; // on définit une variable qui nous permettera de voir si les chiffres entrés par l'utilisateur sont contenus dans l'intervalle 0 , 50

        for (int i = 0; i < nbChoisis.length; i++){

            if (nbChoisis[i] > 0 && nbChoisis[i] <= 50){

                nbCorrect = nbCorrect + 1; // Si le numéros est contenu dans l'intervalle on incrémente nbCorrect

            }

        }

        if (nbCorrect == nbChoisis.length && nbChoisis.length <= 5 && nbEgal == 0) { // On vérifie que tout les nombres sont contenus dans l'intervalle et que l'utilisateur a entré maximum 5 numéros

            int nbGagnants = 0; // On défini une variable nous permettant de compter le nombre de numéros gagnants

            for (int x = 0; x < nbChoisis.length; x++){

                for (int y = 0; y < nbChoisis.length; y++){

                    if (nbChoisis[x] == nbLoto[y]){

                        nbGagnants = nbGagnants + 1; // on incrémente la variable nbGagnants si le numéro est gagnants

                    }

                }

            }

            boolean jackpot = false; // On déclare un boolean Jackpot pour savoir si l'utilisateur a eu e jackpot ou non on l'initialise à non

            if (nbGagnants == nbChoisis.length){ // on vérifie si la variables nbGagnants et égale au nombre total de numéros entrés par l'utilisateur

                jackpot = true; // si oui on dit que jackpot est true

            }

            // on trie les tableaux
            Arrays.sort(nbChoisis);
            Arrays.sort(nbLoto);

            // on affiche les différentes informations
            System.out.println("Numéros du joueur : " + Arrays.toString(nbChoisis));
            System.out.println("Numéros du tirage : " + Arrays.toString(nbLoto));
            System.out.println("Nombre de bons numéros : " + nbGagnants);

            if (jackpot == true){ // si l'utilisateur a le jackpot on affiche le message suivant

                System.out.println("***** JACKPOT *****");

            }

        } else {

            //si l'utilisateur entre des chiffres qui ne correspondent pas à l'intervalle 0 , 50 ou si l'utilisateur a entré plus de 5 chiffres on affiche ce message d'erreur
            System.out.println("Vous devez entrer des nombres entiers entre 0 et 50 et maximum 5 numéros différents.");

        }

    }

}

Can you compile now ?

Yes. It compiles. Still looking at it…

Where is the code that asks for the user input? If this is a Codecademy project, would you share a link to it, please?

This is not a CodeCademy project unfortunately

It looks like you are expecting args to be an array containing the user’s chosen numbers. With only the code you’ve shared, args is not assigned to anything, and has a length of 0, so all of your for loops that use nbChoisis.length as the stop condition are not iterating even once. All of the code in your for loops is skipped because you are iterating from 0 to 0. Is there another class?

Thank you but As I said I’m new on java, could you tell me please where and what I have to change exactly please and sorry for disturbing you

Not without more information. As I said, your code is expecting args to contain the numbers chosen by the user, but it isn’t assigned to anything. Where is the user input supposed to come from?

Okay, so if I provide an array of numbers as strings which is what your code is expecting, your code functions as expected except for where you are checking to see if the user input contains duplicates:

        int nbEgal = 0; // on definie une variable pour vérifier que tout les nombres entrés par l'utilisateur sont differents

        for (int n = 0; n < nbChoisis.length; n++){ // on vérifie que toutes les valeurs entrées par l'utilisateur sont différentes

            for (int m = 0; m < nbChoisis.length; m++){

                if (nbChoisis[n] == nbChoisis[m]){ // **what values are n and m assigned to here?

                    nbEgal = nbEgal + 1;

                }

            }

        }

Look at the line I placed a comment next to. n and m start out with the same value, so you are comparing each number to itself first. Those are obviously the same, so your counting variable, nbEgal gets incremented by 1. This causes your error message to be displayed because your program is incorrectly determining that every number is duplicated in the nbChoisis array.

Consider how you can modify the inner for loop’s index m's starting value, so that you only compare each number to the numbers that come after it in the array rather than comparing it to itself.

After correcting this issue, I get output like this (it varies each time):

Numéros du joueur : [4, 11, 22, 35, 48]
Numéros du tirage : [11, 27, 28, 35, 44]
Nombre de bons numéros : 2

I’m trying to resolve this since 3 hours but I’m not able to get the solution.

Can you for the last time give me the solution please? I’m sorry but I have to give back this exercice tonight and as you can see I really can not resolve

I cannot. I don’t know what your assignment is entirely. I had to re-write the beginning of your code to supply values that weren’t included. Is that part of the assignment? I don’t know. The code you supplied has the issue that I pointed out that keeps it from functioning as expected even with supplied values. If you want to post the instructions for the assignment, I can try to help you, but I can’t just give you a solution when I don’t know exactly what is required, and wouldn’t if I could. That would be cheating.

Well, you’re right, but actually I didn’t succeded to find the index to change you were talking about, how can I do ?

Looking at the above code, since m starts out with a value of 0, the following line:

if (nbChoisis[n] == nbChoises[m])

evaluates to ‘true’ on the first iteration.
You don’t want to compare each number to itself. You want to compare it to the numbers that come after it in the array. Instead of initializing m with a value of 0, what could you do to make sure that it is always 1 more than n?

Hint

What’s one more than n?

Answer:

n + 1

Further Hint (If needed):

Initialize m to n + 1:

for (int m = n + 1; m < nbChoisis.length; m++)