Results 1 to 6 of 6
  1. #1
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Need a little help with arrays

    I have a very hard time understanding arrays. My program is supposed to let the user enter seven numbers, the computer randomly generates seven numbers between 1-39 and then the computer will tell the user which numbers were correct. The thing is I have no idea how to store the correct numbers in a new array, and then print the new array at the end. I've been messing around with all sorts of attempts, but nothing is working. Any guidelines would be highly appreciated!



    Java Code:
    package whatever;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Random;
    import java.util.Scanner;
    
    public class lottery {
    
    	public static void main(String[] args) {
    		
    		Scanner reader = new Scanner(System.in);
    		
    		int[] userNumbers = new int[7];
    		int[] winningNumbers = new int[7];
    		int guesses;
    		int i;
    		int counter = 0;
    		
    		
    		
    		 ArrayList<Integer> list = new ArrayList<Integer>();
    	        for (int x=1; x<40; x++) {
    	            list.add(new Integer(x));
    	        }
    	        Collections.shuffle(list);
    	        for (int x=0; x<7;x++) {
    	           winningNumbers[x] = list.get(x);
    	        }
    		
    		
    		
    		System.out.println("Enter seven numbers between 1 and 39 ");
    		
    		for(i = 0; i < userNumbers.length; i++){
                guesses = reader.nextInt();
                userNumbers[i] = guesses;
               // System.out.println(userNumbers[i]);
               
                for(int x = 0; x<7;x++){
                	
                	if(winningNumbers[x] == userNumbers[i]){
                		
                		
                		counter+=1;
                
                	
                	}
              
                }
                
              
    		}
    		
    		if (counter == 7){
    			System.out.println("You won!");
    		}
    		else
    			System.out.println("You had " + counter + " numbers correctly: "  ); //THIS IS WHERE I WANT THE NUMBERS PRINTED
    		
    			
    			
    			/*System.out.println(Arrays.toString(winningNumbers));
    			System.out.println(counter);
                            These two lines are only so I can see what is happening*/
     
    	}
    
    }
    Last edited by iwtljava; 11-25-2016 at 05:28 PM. Reason: had to

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need a little help with arrays

    What are in the arrays now?
    Here is one easy way to print an array's contents:
    Java Code:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    Post the array's current contents
    and add some comments showing what you want to be in the array.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Need a little help with arrays

    Nothing is in the array because I always get error messages. Basically, at line 45 I want to make a new array, which consists of elements from line 43 (which compares the user's numbers to the computer's numbers, and if the user has a number correct I want to store it in a new array). So I've tried doing it like


    Java Code:
    int[]correctNumbers = winningNumbers[x];
    , but all I get is a Type mismatch: cant convert from int to int[]

    And then at line 60 I want to print the new array so I can see which numbers were correctly guessed.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need a little help with arrays

    I want to store it in a new array
    See lines 15 and 16 for how to declare a new array.

    See lines 29 and 38 for how to store a value in an array.


    When the number of elements is not known when the array is created, you need to define an array that is large enough to hold all the possible elements that will be added. Then to keep track on the number of elements in the array there needs to be an int value that holds the current number of elements in the array.

    Those problems have been solved with the ArrayList class.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    iwtljava is offline Member
    Join Date
    Nov 2016
    Posts
    22
    Rep Power
    0

    Default Re: Need a little help with arrays

    Java Code:
    package whatever;
     
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Random;
    import java.util.Scanner;
     
    public class lottery {
     
        public static void main(String[] args) {
             
            Scanner reader = new Scanner(System.in);
             
            int[] userNumbers = new int[7];
            int[] winningNumbers = new int[7];
            int guesses;
            int i;
            int counter = 0;
            int[]correctNumbers=new int[7];
             
             
             
             ArrayList<Integer> list = new ArrayList<Integer>();
                for (int x=1; x<40; x++) {
                    list.add(new Integer(x));
                }
                Collections.shuffle(list);
                for (int x=0; x<7;x++) {
                   winningNumbers[x] = list.get(x);
                }
             
             
             
            System.out.println("Enter seven numbers between 1 and 39 ");
             
            for(i = 0; i < userNumbers.length; i++){
                guesses = reader.nextInt();
                userNumbers[i] = guesses;
               // System.out.println(userNumbers[i]);
                
                for(int x = 0; x<7;x++){
                     
                    if(winningNumbers[x] == userNumbers[i]){
                         
                        correctNumbers[x] = userNumbers[i];  
                        counter+=1;
                 
                     
                    }
               
                }
                 
               
            }
             
            if (counter == 7){
                System.out.println("You won!");
            }
            else
                System.out.println("You had " + counter + " numbers correctly: " + correctNumbers  ); //THIS IS WHERE I WANT THE NUMBERS PRINTED
             
                 
                 
                /*System.out.println(Arrays.toString(winningNumbers));
                System.out.println(counter);
                            These two lines are only so I can see what is happening*/
      
        }
     
    }
    I added line 20 and 46
    Something like this? All it prints now is the place where the array is stored, [I@1b6d3586

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need a little help with arrays

    All it prints now is the place where the array is stored, [I@1b6d3586
    That is the String returned by the array's toString() method.

    The code in line 65 formats and prints the contents of the counter array.

    You missed this part of my post:
    to keep track on the number of elements in the array there needs to be an int value that holds the current number of elements in the array.
    That variable will be used as the index into the array. The variable: counter might work.
    Last edited by Norm; 11-25-2016 at 07:41 PM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 4
    Last Post: 06-28-2014, 06:00 PM
  2. Copying Single Arrays to 2-D Arrays
    By jmscarlet9 in forum New To Java
    Replies: 7
    Last Post: 04-03-2012, 12:17 AM
  3. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 01:49 PM
  4. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 02:28 PM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •