kerus
kerus

Reputation: 27

trying to print out array from random numbers from another class

I have basically finished this piece of code, but when I print out the numbers from the array in the lottery class, I get a bunch of seeming gibberish. How can I fix the problem?

import java.util.Scanner;
public class Hw5pr2
{
    public static void main(String[] args)
    {
    Scanner kb = new Scanner(System.in);
    int[] rand = new int[5];
    System.out.println("please enter 5 number");
    for (int a = 0; a<rand.length; a++)
    {
    rand[a] = kb.nextInt();
    }
    Lottery k = new Lottery();
    System.out.print("your number are: ");
    for (int a = 0; a < rand.length; a++)
    {
        System.out.print(rand[a]+",");
    }
    System.out.print("The Winning numbers are: ");
    for (int a = 0; a < rand.length; a++)
    {
        System.out.print(k.getArray()+",");
    }
    System.out.println("you have " + k.RanInput(rand) + " matching number!!");
    }
}



import java.util.Random;
public class Lottery
{
private int[] lotteryNumbers = new int[5];
    public Lottery()
    {
    Random rand = new Random();
    for (int a = 0; a<lotteryNumbers.length; a++)
        {
        lotteryNumbers[a] = rand.nextInt(9)+1;
        }
    }
    public int RanInput(int[] Inran)
    {
    int b = 0;
    for (int a = 0; a<lotteryNumbers.length; a++)
        {
        if (lotteryNumbers[a] == Inran[a])
            {
            b++;
            }
        }
    return b;
    }
    public int[] getArray()
    {
    return lotteryNumbers;
    }
}

Upvotes: 0

Views: 175

Answers (4)

not_a_bot
not_a_bot

Reputation: 2362

A few things to note:

As others have mentioned, if you want to print the elements of an array, a simple way to do it is with a for loop. Your code won't work the way you want it to unless you access each element of k's lotteryNumbers array separately:

System.out.print("The winning numbers are: ");
for (int a = 0; a < rand.length; a++)
    {
        System.out.print(k.getArray()[a]); //make sure to include the [a]
        if(a != rand.length - 1)   //added to help make prints more readable
            System.out.print(", ");
    }

Secondly, your RanInput function won't behave the way you want it to, assuming you wanted to see how many of the user's numbers match the lottery numbers. Currently, it just checks if the nth number inputted matches the nth lotto number chosen, which is unlikely to be true. Instead, you should do something like this:

public int RanInput(int[] Inran)
{
    int count = 0;
    for (int i = 0; i<lotteryNumbers.length; i++)
    {
        for(int j = 0; j < lotteryNumbers.length; j++) {
            if (lotteryNumbers[j] == Inran[i])
                {
                count++;
                }
        }
    }
    return count;
}

It compares each of the inputted numbers with each of the lotto numbers. There are more efficient ways to do this than the code above, but I think it should be good enough for now.

I don't think the code you use to generate the lottery numbers account for duplicates (e.g. the numbers might be 1, 2, 2, 1, 3) so you should fix that.

Finally, here's some error checking you should implement if you want to be thorough:

  • make sure the user inputs exactly 5 numbers
  • all positive integers
  • are below some upper bound (say, 100)

Upvotes: 0

user4249431
user4249431

Reputation:

When you print out the array, to the best of my knowledge, it calls the toString method. This returns a hash of the object, not the values it contains. You can call the static method toString from the Arrays class also to print the string values of the array.

        System.out.print(Arrays.toString(k.getArray())+",");

or use a for loop

for (int a = 0; a < k.getArray().length; a++)
    {
        System.out.print(k.getArray()[a]+",");
    }   

Upvotes: 0

user3437460
user3437460

Reputation: 17454

for (int a = 0; a < rand.length; a++)
{
    System.out.print(k.getArray()+","); //k.getArray() which returns you an entire array
}

You are trying to print the entire array. That's why it may show you the reference instead. You can store the array in a temp array and print the contents of that array.


You may try doing something like this:

int[] winningNum = k.getArray();
for (int a = 0; a < winningNum.length; a++)
    System.out.print(winningNum[a] + ",");

Upvotes: 1

subash
subash

Reputation: 3140

try something like

System.out.print("The Winning numbers are: "); for (int a = 0; a < rand.length; a++) { System.out.print(k.getArray()[a]+","); }

note here

 for (int a = 0; a<lotteryNumbers.length; a++)
 {
    if (lotteryNumbers[a] == Inran[a])
    {
        b++;
    }
 }
 return b;

this is always return lotteryNumbers.length-1; beacause always two arrays are same

Upvotes: 0

Related Questions