-2

I am trying to build a lotto machine using java. This is the code:

import java.util.Scanner;
import java.util.Random;
import java.util.Random;

public class wissam {
    public static void main(String[] args) {    
        int counter = 0;    
        int[] lottoNumbers = new int[6];    
        for (int x = 0; x < lottoNumbers.length; x++) {    
                Random rand = new Random();    
                lottoNumbers[x] = rand.nextInt(42) + 1;    
        }    
        int[] userChoice = new int[6];    
        System.out.println("Enter a number");    
        Scanner scan = new Scanner(System.in);    
        userChoice[0] = scan.nextInt();    
        for (int y = 1; y < userChoice.length; y++) {    
                System.out.println("Enter another number");    
                userChoice[y] = scan.nextInt();    
        }    
        for (int x = 0; x < lottoNumbers.length; x++) {    
            System.out.print(lottoNumbers[x] + " ");    
        }    
        System.out.println("");    
        for (int z = 0; z < userChoice.length; z++) {    
            for (int a = 0; a < lottoNumbers.length; a++) {
                if (lottoNumbers[z] == userChoice[a]) {    
                    counter++;    
                    int b = lottoNumbers[z];    
                    System.out.print("The common numbers were" + b + "");    
                }    
            }    
        }    
        if (counter == 0) {    
            System.out.println("You are such a loser");                
        }    
    }                
}

It asks the user to input 6 numbers and works fine, as long as the user chooses 6 distinct numbers between 1 and 42.

I want to check if the user inputs distinct numbers that are between 1 and 42, and if they aren't I want to ask the user to change the number. How do I do that?

Avihoo Mamka
  • 4,367
  • 3
  • 26
  • 39
WissamM
  • 1
  • 1
  • 1
    Have you tried anything yourself? Did you write that code yourself? – f1sh May 09 '17 at 14:35
  • 2
    This actually is not a "we do your homework"-site. – Tobi May 09 '17 at 14:36
  • I think this question has essentially been answered here: http://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner. ---------- – J.P. May 09 '17 at 14:37

3 Answers3

0

I would not use an array. I would use a Set. It has the function 'contains'. So whenever the users gives another number, you can check if the Set so far already contains the number.

GreyFairer
  • 12,327
  • 1
  • 29
  • 50
0

I would do it this way

public static void main(String[] args) {
  final ArrayList<Integer> userChoice = new ArrayList<>(6);

  // Scan numbers
  Scanner scan = new Scanner(System.in);
    while (userChoice.size() < 6) {
      System.out.println("Enter a number");
      int choice = scan.nextInt();

      if (choice < 1 || choice > 42) {
        System.out.println("Number not between 1 and 42");
        continue;
      }

      if (userChoice.contains(choice)) {
        System.out.println("Number already chosen");
        continue;
      }

      userChoice.add(choice);
    }

    System.out.println("You have chosen: " + userChoice);
}
-1

Considering that you're almost there, the simplest case would be to create nested loops and before you store the number, check that it's not already within the array.

for (int y = 1; y < userChoice.length; y++) {

     System.out.println("Enter another number");
     boolean flag = false;
     int result = scan.nextInt();

     if(result < 1 || result > 42){
        System.out.println("please enter a number between 1 - 42 inclusive");
        y--;
        continue;
     }

     for(int i = 1; i <  userChoice.length; i++){
        if(result == userChoice[i]){
           flag = true;
           break;
        }
     }

     if(flag){
        System.out.println("Enter a different number");
        y--;
     }else{
        userChoice[y] = result;
     }

}
Ousmane D.
  • 50,173
  • 8
  • 66
  • 103