user3703289
user3703289

Reputation: 346

How to compare existing numbers from a file with with a random number?

I'm doing an exercise where I have to make a 'Lotto game'.

I've generated 6 random numbers (rand1, rand2, rand3, rand4, rand5, rand6), and now I have to compare them with my existing numbers, which is saved in a file f. I want to compare each random number at a time with the numbers I have saved in my file.

This is what I got so far:

public class lottoEx{

   public static void main(String[] args)throws Exception{

      userNumbers(4, 5, 7, 9, 11, 19);

      drawNumbers();


   }

   public static void userNumbers(int num1, int num2, int num3, int num4, int num5, int num6)throws Exception{ //USER NUMBERS

      File f = new File("lotto.dat");

      PrintStream output = new PrintStream(f);

      output.println(num1 + " " + num2 + " " +  num3 + " " + num4 + " " + num5 + " " + num6);

      Scanner read = new Scanner(f);

      System.out.print("Your numbers: ");

      while(read.hasNext()){

         System.out.print(read.nextInt() + " ");

      }

   }

   public static void drawNumbers()throws Exception{ //RANDOM NUMBERS

      Random rand = new Random();

      int rand1 = rand.nextInt(19)+1;
      int rand2 = rand.nextInt(19)+1;
      int rand3 = rand.nextInt(19)+1;
      int rand4 = rand.nextInt(19)+1;
      int rand5 = rand.nextInt(19)+1;
      int rand6 = rand.nextInt(19)+1;

      Thread.sleep(2000);

      System.out.println();
      System.out.println();

      System.out.print("The lotto numbers are: ");

      Thread.sleep(2000);

      System.out.print(rand1 + " " + rand2 + " " + rand3 + " " + rand4 + " " + rand5 + " " + rand6);

         doCompare(rand1, rand2, rand3, rand4, rand5, rand6);

         winCheck();

   }

   public static void doCompare(int rand1, int rand2, int rand3, int rand4, int rand5, int rand6)throws Exception{

      Scanner scan = new Scanner("lotto.dat");

      Set<Integer> set = new HashSet<>();

      while(scan.hasNextInt()){
         set.add(scan.nextInt());    
      }
      System.out.println(set);

      if(set.contains(rand1)){

         System.out.println("Match on rand1");

      }

   }

   public static void winCheck()throws Exception{

      //code goes here

   }

}

It seems like somethings wrong since it only compares the random number with the first number in my file? Im kind of stuck right now. Hope someone can help me out! :-)

Upvotes: 0

Views: 138

Answers (2)

ifloop
ifloop

Reputation: 8386

Here an attempt using a java.util.Set for storing the generated numbers and using the Scanner constructor, which takes a File as argument.

public static void main(String[] args) throws FileNotFoundException {
    final Set<Integer> lottoResults = new HashSet<>();
    final Random rnd = new Random(System.currentTimeMillis());

    // generate numbers
    while (lottoResults.size() < 6) {
        lottoResults.add(rnd.nextInt(49) + 1);
    }

    final Scanner in = new Scanner(new File("path\\to\\yourLottoFile.txt"));
    for (String s : in.nextLine().split(",")) {
        final int lottoGuess = Integer.parseInt(s);
        // maybe some error handling here (invalid / wrong input)?

        System.out.printf("The guess: %2d is %s\n", lottoGuess, (lottoResults.contains(lottoGuess) ? "correct!!!" : "incorrect"));
    }
}

An example output would look like this:

The guess:  4 is incorrect
The guess: 10 is incorrect
The guess:  7 is incorrect
The guess:  5 is incorrect
The guess: 11 is correct!!!
The guess: 19 is incorrect

Upvotes: 0

Khalid
Khalid

Reputation: 2230

An easy and an efficient way is to store all the numbers in the file in a Set, and then look up the set for each random number. This way you only have to read the file once.

Set<Integer> set = new HashSet<>();

while(scan.hasNextInt()) {
    set.add(scan.nextInt());
}

if (set.contains(rand1)) {
    System.out.println("MATCH on rand1: " + rand1);
}

// repeat for rand2, rand3..etc

Upvotes: 1

Related Questions