Hi there, as one of my first programs in java, I thought I will do a simple (!) lottery program...needless to say it doesn't compile, and after looking at it for quite awhile trying to unsuccessfully spot the error (I am not sure how to debug in netbeans, any advice on that very welcome too of course) I thought that it is time for some help. here's the program (compiled with netbeans):

package lottery;

import static java.lang.System.out;
import java.util.Scanner;
import java.util.Random;
public class Lottery {

    public static void main(String[] args) {
         int[] numbers = new int[49];

                                //int[] numbers = new int[49];
        int[] winningNumbers = new int[6]; //array holding 6 random numbers
        int[] userNumber = new int[6]; //array holding the input
        Scanner theNumbers = new Scanner(System.in);
        int guesses;
        int counter;
        int i;

        /*
        for(i = 0; i <  numbers.length; i++){
                numbers[i] = i + 1;
           out.println(numbers[i]);
           }
        */

        //generate 6 random numbers

        for(i = 0; i < winningNumbers.length; i++ ){
                int randomNums = new Random().next(49) + 1;
                winningNumbers[i] =  randomNums;                               
        }

        out.println("Enter the 6 numbers");
        for(i = 0; i < userNumber.length; i++){
                guesses = theNumbers.nextInt();
                userNumber[i] = guesses;
                out.println(userNumber[i]);
                if(winningNumbers[i] == userNumber[i] ){
                  counter+=1;
                }
         }
        if (counter == 6){
            out.println("you won!! COngratulations!");
        else
            out.println("you lose!");
        }
    }
}

SO the idea behind this is that the computer generates 6 random number in the range of 1 to 49, and store them in an array, the user inputs his own 6 guesses, stores them into an array, then each entry is compared to what the computer has stored and for each right guess the counter is incremented by 1. If the counter is 6 then you win, if not you lose.
Now, netbeans is giving me some strange errors and warnings:
1) with this import java.util.Scanner; it says thatthis doesn't correspond to the specified code style...?!
2)here int randomNums = new Random().next(49) + 1; it tells me that next(Int) has protected access, whatever that means;
3) at the else level it says: 'else' without 'if'...ehm?!
4) at counter+=1;it says variable counter might not have been initialised
5) the last } says "class, inteface or enum expected"

Any idea what I have done wrong? Please take in consideration that as I said I am very new to Java
thanks

Recommended Answers

All 5 Replies

I found these compilation problems

line 16 -->int counter;// this should  be int counter=0;// initialization required for local variable
line 29 --> new Random().next(49);// this should be new Random().nextInt(49)
line 42 --> if (counter == 6){ //remove the "openingflower bracket" -->if (counter == 6)
line 48 -->} not required  , means remove line 48 .

Thanks I will make the amendments, try and post back the results. I would like to clarify one thing if I may:
I thought that a variable initialization in java isn't not mandatory,but I seem to understand from your amendments that it is
Fair enough for the other erros, all things I should have been more careful with, thank you very much for pointing them out, much appreciated

right, changes made, here's the amended version:

package lottery;

import static java.lang.System.out;
import java.util.Scanner;
import java.util.Random;
public class Lottery {

    public static void main(String[] args) {
         int[] numbers = new int[49];

                                //int[] numbers = new int[49];
        int[] winningNumbers = new int[6]; //array holding 6 random numbers
        int[] userNumber = new int[6]; //array holding the input
        Scanner theNumbers = new Scanner(System.in);
        int guesses;
        int counter = 0;
        int i;

        /*
        for(i = 0; i <  numbers.length; i++){
                numbers[i] = i + 1;
           out.println(numbers[i]);
           }
        */

        //generate 6 random numbers

        for(i = 0; i < winningNumbers.length; i++ ){
                int randomNums = new Random().nextInt(49) + 1;
                winningNumbers[i] =  randomNums;                               
        }

        out.println("Enter the 6 numbers");
        for(i = 0; i < userNumber.length; i++){
                guesses = theNumbers.nextInt();
                userNumber[i] = guesses;
                out.println(userNumber[i]);
                if(winningNumbers[i] == userNumber[i] ){
                  counter+=1;
                }
         }
        if (counter == 6) {
            out.println("you won!! COngratulations!");
        }
        else    
            out.println("you lose!");

        }

}

This compiles ok but there is a problem. In the console when I input the numbers, the number I input gets printed out straight away, so I end up with the same number twice. Maybe a screenshot clarifies things. http://antobbo.webspace.virginmedia.com/various_tests/lotteryOutput.png
Say the numbers I input are these:
1
2
3
4
5
6
Have a look at the screenshot and you will see this situation in the console :
1
1
2
2
3
3
4
4
5
5
6
6

SO for each number the program prints it out straigh away, no idea why
thanks

@Violet_82
There is no problem.
The first value being printed is the user entered value from key board ( which is the the input from user to program).
The second value is from printed from code/program .

Have a look at lines 35 to 37

 35   guesses = theNumbers.nextInt();// here the value is taken from keyboaord user entered value and printed on console as soon as user enters any number.
 36                  userNumber[i] = guesses;
 37                out.println(userNumber[i]);// here same user-entered value is being printed console again 

Please comment line number 37, since it is not required.

** We have observed user entered value is printed already on console at line 35 .**

This will definitely solve your problem of "values printed twice on console"

Happy coding.

Ah, what a donkey I am! Sorry, didn't think about that!
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.