123
123

Reputation: 11

How to compare two arrays. One array taking from a file and the other array are randomnumber?

This is what i got. I can see the two arrays but when comparing I got a wrong answer.I get a lot of false as output when compiling and running but insted of 36 result(boolean) I get 180 boolean result.

import java.io.*;
import java.util.*;



public class Lotto {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws FileNotFoundException
{
    userNumbers();
    drawNumbers();  
}
public static void userNumbers()throws FileNotFoundException
{
     Scanner in = new Scanner(new FileReader("lotto.dat")); //In this file there is 6 integer

 while(in.hasNext())
 {
     if(in.hasNextInt())
     {
        //in.next();
        System.out.println(in.nextLine());
     }
     else
     System.out.print(in.next()+ " ");
 }

}
public static void drawNumbers() throws FileNotFoundException
{
     int N = 6;// 6 Random Numbers
     int[] lottoNumber = new int[N];
     int[] userNumber = new int[N];
     //This generate random numbers of size N
     for (int i = 0; i < N; i++) 
     {
        Random n = new Random(); 
        lottoNumber[i] = n.nextInt(20)+1;
        System.out.print(lottoNumber[i]+" ");           
     }

     Scanner dat = new Scanner(new FileReader("lotto.dat"));
        int i=0;
        while(dat.hasNextInt())
        {
           userNumber[i++]=dat.nextInt();

           System.out.println("");
           doCompare(lottoNumber, userNumber); 
        }


}
  public static void doCompare(int[] a, int[] b) {
   for(int i = 0; i < a.length; i++)
  {
for(int j = 0; j < b.length; j++)
{
    if(a[i] == b[j])
    {
        System.out.print("True");//true
    }
    else
    {
        System.out.print("false");``
    }
}
}}}

Upvotes: 0

Views: 76

Answers (4)

Rishav Basu
Rishav Basu

Reputation: 401

You are comparing before populating userNumber with all the numbers from the file. You just have to move the statement doCompare(lottoNumber, userNumber) outside the while loop :

while (dat.hasNextInt()) {
       userNumber[i++] = dat.nextInt();

  System.out.println("");
  //doCompare(lottoNumber, userNumber);
}

doCompare(lottoNumber, userNumber);

Upvotes: 2

kai
kai

Reputation: 6887

For me it prints 216 boolean values, because the function which should print 36 is executed 6 times, for each integer in your lotto.dat you only have to change:

while(dat.hasNextInt())
{
   userNumber[i++]=dat.nextInt();
   System.out.println("");
   doCompare(lottoNumber, userNumber); 
}

to

while (dat.hasNextInt())
{
    userNumber[i++] = dat.nextInt();
}
System.out.println();  //<-- Do the newline out of the loop too
doCompare(lottoNumber, userNumber);

now your output will be one line with 36 booleans:

falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalseTruefalsefalsefalseTruefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse

Upvotes: 2

Christian
Christian

Reputation: 1586

Since you are running your doCompare-Function every time when you read the next number from file, you are running this routine 6 * 36 times; since you only get 180 results, it looks like you do not have 6 numbers in your file, but most likely only 5!

Move your doCompare()-call outside of the while (dat.hasNextInt())-loop and you should be fine!

Upvotes: 1

Jason C
Jason C

Reputation: 40335

You have this:

while(dat.hasNextInt())
{
   userNumber[i++]=dat.nextInt();
   System.out.println("");
   doCompare(lottoNumber, userNumber); 
}

You are running the comparison every time you read a number from the file. What it sounds like you want to do is read everything from the file and then run the comparison - and so you should do just that. What you probably mean is this:

while(dat.hasNextInt())
{
   userNumber[i++]=dat.nextInt();
   System.out.println(""); // <- This probably isn't useful, either.
}
doCompare(lottoNumber, userNumber); 

By the way, if you are getting 180 results, since 5 * 36 = 180 that implies that you are only reading 5 numbers from the file. Are you sure the file has 6 numbers in it, or did you actually get 216 results?

Upvotes: 1

Related Questions