Results 1 to 2 of 2
  1. #1
    arfor is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Help passing arrays to other methods

    Basically, what my program is supposed to do is generate 5 random lottery numbers, get input from the user for guesses for the numbers, then see if any of the guesses from the user are right.
    I'm having errors when referencing the arrays, the values aren't being passed to the methods right.
    Here's my code:
    Java Code:
    import java.util.*;
    public class lottery {
    	public static void lotteryNums(){
    
    		Random randnum=new Random();
    		int[] lotteryNumbers=new int[5];
    		
    		for (int i=0; i<lotteryNumbers.length; i++)
    			{
    //have to make the max value 51 to include 50 when using nextint
    			lotteryNumbers[i]=(randnum.nextInt(51)+0);
    			}
    //sort lotterynumbers ascending
    		Arrays.sort(lotteryNumbers);
    	
    //check lotterynumbers
    		System.out.println("Winning numbers: " + Arrays.toString(lotteryNumbers));
    	}
    
    	public static void lottocheck(int[] Guess)
    	{
    	
    	
    		for (int count1=0; count1<5; count1++)	
    		{
    			for (int count2=0; count2<5; count2++)
    			{
    //getting an error here for lotteryNumbers, won't recognize array
    				if (Guess[count1]==lotteryNumbers[count2])
    					{
    						int matches=matches+1;
    						System.out.println("You have "+ matches + " matches!");
    					}
    			}
    		}
    	}
    }
    
    
    
    
    import java.util.Arrays;
    import java.util.Scanner;
    public class lab1 {
    	public static void main(String[] args)
    	{
    		getGuess();
    		lottery lotto=new lottery();
    		lotto.lotteryNums();
    
    //Getting an error here for guesses[], won't recognize array
    		lotto.lottocheck(guesses[]);
    	
    	
    	}
    	public static void getGuess(){
    		Scanner input = new Scanner(System.in);
    		int[] guesses= new int[5];
    		System.out.println("Enter your 5 lottery guesses below:");
    		for (int i=0; i<5;i++){
    			guesses[i] = input.nextInt();
    		}
    	//sort guesses
    	Arrays.sort(guesses);
    	//check guesses
    	System.out.println("Guesses sorted: " + java.util.Arrays.toString(guesses));
    
    	}
    }
    Can anyone help me out? How do I get this working?
    Last edited by arfor; 02-03-2013 at 08:51 PM.

  2. #2
    Estrella is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    16
    Rep Power
    0

    Default Re: Help passing arrays to other methods

    In line 6, 'lotteryNumbers' is declared inside the lotteryNums() method. this makes the scope of 'lotteryNumbers' that method only. That is, lottocheck() doesn't know 'lotteryNumbers' exists. You probably want to make 'lotteryNumbers' a class variable.

Similar Threads

  1. Passing variables to methods
    By KKorn429 in forum New To Java
    Replies: 3
    Last Post: 05-21-2012, 12:40 AM
  2. Passing arrays to function
    By boys21 in forum New To Java
    Replies: 1
    Last Post: 11-20-2011, 10:29 PM
  3. Arrays and passing values to methods help please.
    By toppcon in forum New To Java
    Replies: 21
    Last Post: 07-28-2011, 05:05 AM
  4. passing arguments to other methods
    By popeyito18 in forum New To Java
    Replies: 2
    Last Post: 07-02-2011, 11:01 PM
  5. generics and passing arrays
    By TopNFalvors in forum New To Java
    Replies: 0
    Last Post: 03-17-2011, 04:19 PM

Posting Permissions

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