I need help, The array in Class Loto generates 6 random numbers and print them out. the Class Lotto askes the user to input 6 numbers which are stored in an array. i need code to compare the two arrays and see if they equal each other, and if they do display how many i got right. PLEASE HELP

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Loto {

 public static void main(String[] arg) 
 {
  ArrayList<Integer> al = new ArrayList<Integer>();
  for(int i = 1; i <= 49; i++)
   al.add(i);
  Random ran = new Random();
  
  for(int i = 0; i < 6; i++) 
  {
   int x = al.remove(ran.nextInt(al.size()));
   System.out.print(" " + x);
  }
  System.out.println();
 }
}
import java.util.*;
import java.util.Scanner;
class lotto{
  public static void main(String[]args){
    int f[] = new int[6];
   
    
    Scanner keyboard = new Scanner(System.in);
    
    for(int j=0; j<f.length; j++){
      System.out.print("Input your " + (f[0]+1) + "st lotto number: ");
      f[0] = keyboard.nextInt();
      
    }
    
  }
}

Recommended Answers

All 5 Replies

I need help, The array in Class Loto generates 6 random numbers and print them out. the Class Lotto askes the user to input 6 numbers which are stored in an array. i need code to compare the two arrays and see if they equal each other, and if they do display how many i got right. PLEASE HELP

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Loto {

 public static void main(String[] arg) 
 {
  ArrayList<Integer> al = new ArrayList<Integer>();
  for(int i = 1; i <= 49; i++)
   al.add(i);
  Random ran = new Random();
  
  for(int i = 0; i < 6; i++) 
  {
   int x = al.remove(ran.nextInt(al.size()));
   System.out.print(" " + x);
  }
  System.out.println();
 }
}
import java.util.*;
import java.util.Scanner;
class lotto{
  public static void main(String[]args){
    int f[] = new int[6];
   
    
    Scanner keyboard = new Scanner(System.in);
    
    for(int j=0; j<f.length; j++){
      System.out.print("Input your " + (f[0]+1) + "st lotto number: ");
      f[0] = keyboard.nextInt();
      
    }
    
  }
}

well first i'd suggest changing the users int[] to an arraylist of integers also. next sort each array list check here:http://www.javamex.com/tutorials/collections/sorting_simple_cases.shtml , after you have sorted them iterate through arrays comparing each value:http://www.coderanch.com/t/407003/java/java/Compare-two-ArrayList

when i changed the user int[] to array list, how do i change the f[0] = keyboard.nextint() to work with array list

maybe:

int n=keyboard.nextint();
arraylistname.add(n);

Alright i got it to compile but i can't get the arrays to compare, if you read the code I'm trying to compare the arrays with an if loop, inside of 2 for loops. if you could help that would be great

import java.util.*;
import java.util.ArrayList;
import java.util.Scanner;
class lotto
{
  
    public static int getUse(int[] f)
    {
      
      //int Array_Size = 6;
     // int[] f = new int[Array_Size];
      int User = f[0];
      
      Scanner keyboard = new Scanner(System.in);
      
      for(int j=0; j<f.length; j++)
      {
        System.out.print("Enter your Number" + (j + 1) + "number:");
        f[j] = keyboard.nextInt();
        
      }
      System.out.print("numbers you have chosen");
      for (int i=0; i<f.length; i++)
      {
        System.out.printf(" " +(f[i]) );
        System.out.println();
      }
      return User;
    }
   public static int compair(int[] u,int[] r)
  {
     int user = 0;
     int ran =0;
     for (int i = 0; i < 6; i++)
     {
     user = u[i];
       for (int j = 0; j < 6; j++)
       {
       ran  = r[j];
       
       if (r[j] == u[i])
       {
         System.out.println("your number matches");
       }
       else 
       {
         System.out.println("your numbers do not match " + (i+1) );
       }
     }
     }
     return user;
  }
   
  
}
import java.util.*;

public class LotteryTest
{
 LotteryTest() 
 {
  LotoNumber ln = new LotoNumber(1, 49);

  int drawing[] = new int[6]; // array of frequency counters

//  pick 6 numbers
  for (int number = 0; number < drawing.length; number++ )
   drawing[number] = ln.getNextNumber();

//  output each array value
  System.out.printf ( "Numbers: ");
  for ( int number = 0; number < drawing.length; number++ )
   System.out.printf (" " + drawing[number]);
  System.out.println();

 }

 public static void main(String args[])
 {
    int ARRAY_SIZE = 6;
    int [] u = new int[ARRAY_SIZE];
    int [] r = new int[ARRAY_SIZE];
  lotto.getUse(u);
  new LotteryTest();
  lotto.compair(u,r);
 } // end main

 class LotoNumber 
 {
  // ArrayList to hold the possible numbers
  private ArrayList<Integer> al;
  // Random number generator
  private Random ran;

  // constructor you specify as parameters the range of numbers 1 36
  public LotoNumber(int from, int to) 
  {
   // random number generator
   ran = new Random();
   // the arraylist for the values
   al = new ArrayList<Integer>();
   // fill the array list with all the permitted values
   for(int i = from; i <= to; i++)
    al.add(i);
  }

  // returns a randomly selected number
  public int getNextNumber() {
   if(al.size() == 0)
    throw new IllegalStateException("All the Loto numbers have already been picked up");

   return al.remove(ran.nextInt(al.size()));
  }
 } 
} // end class

numbers you have chosen 34
34
43
43
34
34
Numbers: 33 37 3 16 15 48
your numbers do not match 1
your numbers do not match 1
your numbers do not match 1
your numbers do not match 1
your numbers do not match 1
your numbers do not match 1
your numbers do not match 2
your numbers do not match 2
your numbers do not match 2
your numbers do not match 2
your numbers do not match 2
your numbers do not match 2
your numbers do not match 3
your numbers do not match 3
your numbers do not match 3
your numbers do not match 3
your numbers do not match 3
your numbers do not match 3
your numbers do not match 4
your numbers do not match 4
your numbers do not match 4
your numbers do not match 4
your numbers do not match 4
your numbers do not match 4
your numbers do not match 5
your numbers do not match 5
your numbers do not match 5
your numbers do not match 5
your numbers do not match 5
your numbers do not match 5
your numbers do not match 6
your numbers do not match 6
your numbers do not match 6
your numbers do not match 6
your numbers do not match 6
your numbers do not match 6

print the value of r[j] in the loop and you'll see that all its element's value is 0

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.