Results 1 to 10 of 10
  1. #1
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Question array with specific needs

    Im using java applet and i need to fill a one dimensional array that has 16 cells with numbers 0-7. The array must be filled randomly and i can only use each number twice. Please include an example code. :)

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Please include an example code.
    It doesn't work that way...at what point are you having a problem? Do you know how to create a random value? Do you know how to create an array of said length? Write some code and ask a specific question about the code, this will result in more informative feedback

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    What have you tried so far?
    Start with a small part of the exercise and add to it. For example can you generate random numbers in the range 0-7?
    Then add an array of counters to keep track of how many of each number you have used.

    BTW We normally don't provide code until you've shown your efforts.

  4. #4
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    5

    Default

    Yeah buddy. This is a community and they're super helpful, but you do have to show that you attempted something. This isn't www.domyJavaprogrammingforme.com .

    As Norm said, start with the basics. If you can describe what you want your code to do, then try doing each individual steps. For the random numbers, you can use the method Math.random(). If you read the API, it will tell you that it only generates numbers from 0.0 to 1.0 (excluding 1.0), so you can mutilply it by 10 and cast it as an integer. You might need to write a conditional to verify that only numbers that are less or equal to 7 are returned, and so on and so forth. I am fairly new as well and it helps to really think about your coding with small steps, projects and effort.

  5. #5
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    This is my program

    int[] imgNum = new int [16];
    int randomNum;
    int appear = 0;
    int flag;


    /*for (int i = 0 ; i < imgNum.length ; i++)
    {
    for (int j = 0 ; j < imgNum.length ; j++)
    {
    randomNum = (int) (Math.random () * 7);
    while (flagExit == 0)
    {

    }
    } //tests each value of the array against the random number
    } //goes through the array storing each value

    for (int i = 0 ; i < imgNum.length ; i++)
    {
    c.println (imgNum [i]);
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Have you printed out some of the random numbers that are generated to be sure that they are in the desired range?

    Now write code to store the first 16 random numbers in the array.
    An easy way to see the contents of the array is to use the Arrays.toString() method. It will print out the whole array on one line.

    After that works, then we'll work on getting a max of two of the same numbers.

  7. #7
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    Okay now i have this i thought it would work but it doesn't:

    PHP Code:
    for (int i = 0 ; i < imgNum.length ; i++)
            {
                imgNum [i] = (int) (Math.random () * 7);
                for (int j = 0 ; j < imgNum.length ; j++)
                {
                    if (imgNum [i] == 0)
                    {
                        count [0]++;
                    }
                    else if (imgNum [i] == 1)
                    {
                        count [1]++;
                    }
                    else if (imgNum [i] == 2)
                    {
                        count [2]++;
                    }
                    else if (imgNum [i] == 3)
                    {
                        count [3]++;
                    }
                    else if (imgNum [i] == 4)
                    {
                        count [4]++;
                    }
                    else if (imgNum [i] == 5)
                    {
                        count [5]++;
                    }
                    else if (imgNum [i] == 6)
                    {
                        count [6]++;
                    }
                    else if (imgNum [i] == 7)
                    {
                        count [7]++;
                    }
                    else
                    {
                        // do nothing
                    }
    
                    if (count [0] > 2 || count [1] > 2 || count [2] > 2 || count [3] > 2 || count [4] > 2 || count [5] > 2 || count [6] > 2 || count [7] > 2)
                    {
                        imgNum [i] = (int) (Math.random () * 7);
                    }
                }
            } // end for

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Can you explain your logic for the code you posted?

    Do you see a pattern in the code you posted?
    Java Code:
    [COLOR="orange"]imgNum [i][/COLOR] ==[COLOR="red"] 0[/COLOR]) 
                    { 
                        count [COLOR="red"][0[/COLOR]]++;
    The orange could replace the red as the array index:
    count[imgNum[i]]++;
    Last edited by Norm; 06-21-2011 at 03:52 AM.

  9. #9
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    PHP Code:
    int[] count = new int [8]; // array that makes sure there arent more than 2 of the same number
            int[] imgNum = new int [16]; // array numbers stored in
            int randomNum; 
    
            for (int i = 0 ; i < count.length ; i++) // make sure count = 0 at startup
            {
                count [i] = 0;
            }
    
            for (int i = 0 ; i < imgNum.length ; i++)
            {
                randomNum = (int) (Math.random () * 7); // make random number
    
                if (randomNum == 0)// find value of number and at the count of how many times thats been on the screen
                {
                    count [0]++;
                }
                else if (randomNum == 1)
                {
                    count [1]++;
                }
                else if (randomNum == 2)
                {
                    count [2]++;
                }
                else if (randomNum == 3)
                {
                    count [3]++;
                }
                else if (randomNum == 4)
                {
                    count [4]++;
                }
                else if (randomNum == 5)
                {
                    count [5]++;
                }
                else if (randomNum == 6)
                {
                    count [6]++;
                }
                else if (randomNum == 7)
                {
                    count [7]++;
                }
                else
                {
                    // do nothing
                }
    
                if (count [0] > 2 || count [1] > 2 || count [2] > 2 || count [3] > 2 || count [4] > 2 || count [5] > 2 || count [6] > 2 || count [7] > 2)//if this applies the number cant go in the array cause its the 3rd of its kind 
                {
                    i--; // to make the loop repeat itself
                    count [randomNum]--; // to make sure the count of how many times that number has been there is set back to 2
                    
    
                }
                else if (count [0] <= 2 || count [1] <= 2 || count [2] <= 2 || count [3] <= 2 || count [4] <= 2 || count [5] <= 2 || count [6] <= 2 || count [7] <= 2)// make the radnom number go into the array
                {
                    imgNum [i] = randomNum;
    
                }
            } // end for i
    
            for (int i = 0 ; i < imgNum.length ; i++)
            {
                c.println (imgNum [i]); // test to work it doesnt yet
            }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Your code has too many repeated almost identical lines. When you see a pattern like this you shoud try to collapse them into a few lines.
    You documented your code but didn't really explain your logic.
    Maybe something like this:
    Get a random number range 0-7.
    Check if we have 2 of those numbers -> here you use the random number as an index to the count array
    If we do, go back and get another random number
    if not, put the number in the output array and increment the array's index and add one to the count array
    if more needed, go back to Get a random number

Similar Threads

  1. How to allow specific letters only?
    By ctmarco3 in forum New To Java
    Replies: 7
    Last Post: 04-01-2011, 05:41 AM
  2. Getting specific hash in a 2D-Array
    By benn22 in forum New To Java
    Replies: 0
    Last Post: 03-18-2011, 08:24 AM
  3. How to: Select specific array indices?
    By louist in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 11:21 PM
  4. Specific Functions for URL's
    By lyric0n in forum Java Servlet
    Replies: 3
    Last Post: 04-06-2010, 01:34 PM
  5. Replies: 2
    Last Post: 11-08-2008, 11:40 AM

Tags for this Thread

Posting Permissions

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