Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Default How to check if a specific value is present in an array?

    Hi there,

    I am creating a Yahtzee game. Every time I press the button "Roll The Dice" the values in my array change. The array contains 6 values (values between 1 to 6) for each of the six dice that are thrown. I have created a button that if it is clicked will count the number of "1" values in the array. How do I do such a thing? I'm a beginner in Java.

    Thank you!

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: How to check if a specific value is present in an array?

    You declare a local variable that contains the number of occurrences (will be of type int I guess) and initialize it.
    You loop through your array and within the loop (e.g. either for(...) or a while(...) loop) you use an if(...) statement to check if the current sample of your array is of the expected value.
    If it is true then you increment your variable.
    After the loop, the variable contains the number of occurrences of the specific number in the array.
    Last edited by Sierra; 01-05-2013 at 12:55 PM.
    I like likes!

Similar Threads

  1. getting specific information from the array list
    By elvis0288 in forum New To Java
    Replies: 7
    Last Post: 03-05-2012, 12:40 PM
  2. array with specific needs
    By BeginnerProgrammer in forum Java Applets
    Replies: 9
    Last Post: 06-21-2011, 03:14 AM
  3. How to check if a specific data in a collum
    By mike_ledis in forum JDBC
    Replies: 0
    Last Post: 04-27-2011, 02:09 PM
  4. Getting specific hash in a 2D-Array
    By benn22 in forum New To Java
    Replies: 0
    Last Post: 03-18-2011, 07:24 AM
  5. How to: Select specific array indices?
    By louist in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 10:21 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
  •