Hi guys,
i've been stuck on one part of an assignment where i have to call a number 6 times from a different method/class.

I thought maybe doing a for loop calling its 6 times but i'm not sure how to call the method into the other method, this is what i have so far

private String getNextLottoNumber(String existingLottoNumbers){
		int lottoNumber = 0;
		boolean nextLottoNumber = false;
		while (!nextLottoNumber){
			lottoNumber = (int)(Math.random() * 40 + 1);
			nextLottoNumber = checkIfUnique(lottoNumber, existingLottoNumbers);
		}

		return lottoNumber + "";
	}

	private boolean checkIfUnique(int lottoNumber, String existingLottoNumbers) {
		String uniqueNumber = " " + lottoNumber + " ";
		existingLottoNumbers = " " + existingLottoNumbers + " ";
		int checkNumber = existingLottoNumbers.indexOf(uniqueNumber);
		if(checkNumber == -1){
	  		return true;
		}else{
	  	 	return false;
  		}
	}

	private String getLottoNumbersString() {
		return "";
	}

it all runs fine i just don't know how to then call the unique number 6 times in the getLottoNumbersString I would know how to do it if the lotto numbers were an array but we've been told not to use arrays so now im clueless, does anyone have n e pointers or hints as to how to do this?

thanks

I don't know that your problem has anything to do with the public vs. protected vs. private modiiers in your class, so the thread title may be inaccurate.

There are a variety of ways of getting 6 unique numbers. First, get rid of the Strings. You have to parse it every time to get your numbers anyway. Why not just have an array or Collection (i.e. ArrayList) of numbers? If you're stuck with a String, I suppose you could have a function that parses a String into an ArrayList and/or vice versa as helper functions. But the main methodology should be using plain old numbers.

You could use a loop or you could use some type of recursion. Frankly I'd just use the loop if I wasn't required to use recursion. It looks like you need recursion, possibly, or you're at least required to have the function add ONE new number at a time? Not entirely sure. But if it was me, I wouldn't use recursion. I'd use a loop:

public static ArrayList<Integer> GetUniqueRandomNumbers (int numNumbers, int min, int max)
{
    // declare a anew ArrayList called randomList
    for (int i = 0; i < numNumbers; i++)
    {
        int newNumber = 0;

        do
        {
            newNumber = /* generate random number between min and max */
        }
        while (NumberNotInList (newNumber, randomList));

        // newNumber is not in randomList yet, so add it.
    }

    return randomList;
}

public static void NumberNotInList (int number, ArrayList<Integer> randList)
{
    // returns true if number is in randList, false otherwise
}

I made the methods public and static because they don't require any class data fields, so they don't need to be non-static. Depending on your needs, you may want a private function that calls the public static function above, and you might need some ArrayList to String and/or vice versa function too.

And if you're required to have recursion or required to have a function that takes an ArrayList and adds one element, you'd have to tweak it. But the above is the way I'd do things if it were left to me. Having Strings involved just complicates things, in my view. Don't have 'em if you can help it.

hey thanks, after playing around a bit more and reading what you said and also having a sudden brainy idea i got it working!! like this:

private String getNextLottoNumber(String existingLottoNumbers){
		int lottoNumber = 0;
		boolean nextLottoNumber = false;
		while (!nextLottoNumber){
			lottoNumber = (int)(Math.random() * 40 + 1);
			nextLottoNumber = checkIfUnique(lottoNumber, existingLottoNumbers);
		}

		return lottoNumber + "";
	}

	private boolean checkIfUnique(int lottoNumber, String existingLottoNumbers) {
		String uniqueNumber = " " + lottoNumber + " ";
		existingLottoNumbers = " " + existingLottoNumbers + " ";
		int checkNumber = existingLottoNumbers.indexOf(uniqueNumber);
		if(checkNumber == -1){
	  		return true;
		}else{
	  	 	return false;
  		}
	}
	private String getLottoNumbersString() {
		String allLottoNumbers = "";
			for(int i=0; i<6; i++){
				allLottoNumbers += " " + getNextLottoNumber(allLottoNumbers) + " ";
			}
		return allLottoNumbers;
	}

that seemed to do the trick!!
thanks for your help!!

This article has been dead for over six months. Start a new discussion instead.